Kindergarten Counting Game
| Kindergarten Counting Game |
Everybody sit down in a circle. Ok. Listen to me carefully.
``Woooooo, you scwewy wabbit!''
Now, could someone tell me how many words I just said?
Input and Output
Input to your program will consist of a series of lines, each line containing multiple words (at least one). A ``word'' is defined as a consecutive sequence of letters (upper and/or lower case).
Your program should output a word count for each line of input. Each word count should be printed on a separate line.
Sample Input
Meep Meep! I tot I taw a putty tat. I did! I did! I did taw a putty tat. Shsssssssssh ... I am hunting wabbits. Heh Heh Heh Heh ...
Sample Output
2 7 10 9
题意:给你一句话叫你算出有多少个单词。
#include <stdio.h>
#include <ctype.h>
int main() {
char ch;
int cnt = 0;
int flag = 0;
while((ch = getchar()) != EOF) {
if(ch != '\n') {
if(isalpha(ch)) {
if( !flag) {
flag++;
cnt++;
}
}
else {
flag = 0;
}
}
else {
printf("%d\n",cnt);
cnt = 0;
}
}
return 0;
}
本文介绍了一个简单的程序,用于统计输入字符串中的单词数量。通过逐字符读取并利用字母判断来实现单词计数,适用于多行文本输入。
2323

被折叠的 条评论
为什么被折叠?



