1805 - Run-Length Encoding   

Description

修改下面的程式片段
讓它能夠計算使用者輸入的連續英文字母個數 
(類似 Run-Length Encoding, http://en.wikipedia.org/wiki/Run-length_encoding)
輸入的字元只會有小寫英文字母和空格(' ') 或換行 ('
')
輸入的結尾是 '#' 字元

例如:
輸入 
    aa bbb b c dd a a aa#
輸出
    a2b4c1d2a4

或是輸入
    cccc cc
    cc ddd xx xii #
輸出
    c8d3x3i2


底下的程式樣本
ch 用來記住剛讀取到字元
prev 用來記住目前為止的連續字元
count 則是記錄連續的次數
只要在 /* your code */ 的地方加入你的程式碼
就能夠完成這項作業

[提示]
1. 如果剛讀取到的字元是 ' ' '
' 就忽略
2. 如果剛讀取到的字元和目前連續的字元相同,只需要增加 count
3. 如果剛讀取到的字元和目前連續的字元不同,要重設相關的變數

----------------

/*
ps2_1.c
Input:
    aa bbb b c dd a a aa#
Output:
    a2b4c1d2a4
Input:
    cccc cc    cc ddd xx xii #
Output:
    c8d3x3i2
*/
#include 
int main(void)
{
    int ch;
    int prev = 0;
    int count = 0;

    while ((ch=getchar())!='#') {
        /* your code */
    }
    /* your code */

    return 0;
}




 

Input

參照題目敘述

Output

參照題目敘述

Sample Input  Download

Sample Output  Download

Tags




Discuss