地址:
http://ac.jobdu.com/problem.php?pid=1182
題目描述:
編一個程序,讀入用戶輸入的,以“.”結尾的一行文字,統計一共有多少個單詞,並分別輸出每個單詞含有多少個字符。
(凡是以一個或多個空格隔開的部分就爲一個單詞)
輸入:
輸入包括1行字符串,以“.”結束,字符串中包含多個單詞,單詞之間以一個或多個空格隔開。
輸出:
可能有多組測試數據,對於每組數據,
輸出字符串中每個單詞包含的字母的個數。
樣例輸入:
hello how are you.
樣例輸出:
5 3 3 3
來源:
2002年華中科技大學計算機研究生機試真題
源碼:
#include<stdio.h>
char str[ 10000 ];
int main(){
while( gets( str ) ){
int i = 0;
int lenWord = 0; //統計每個單詞的長度
while( str[ i ] != '.' ){ //遍歷字符串,直到遇到'.'
if( str[ i ] != ' ' ){ //如果這個字符不是空格
lenWord ++;
}
else{ //如果這個字符是空格
if( lenWord != 0 ){
printf( "%d ", lenWord );
}
lenWord = 0; //單個單詞的長度清0
}
i++; //下一個字符
}
//通過上面的循環,最後一個單詞無法輸出
if( lenWord != 0 ){
printf( "%d\n", lenWord );
}
else{
printf( "\n" );
}
}
}
/**************************************************************
Problem: 1182
User: 螺小旋
Language: C++
Result: Accepted
Time:10 ms
Memory:1032 kb
****************************************************************/