首页 > 代码库 > 494 - Kindergarten Counting Game
494 - 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
27109
--------------------------------------------------------------------------------------------
本题主要是判断一行字符串中有多少个word。判断方法为,一个字母开头到不是字母的字符结束为一个词。注意:也许一行的开头就是非字符
1 #include<stdio.h> 2 #include<string.h> 3 #include<ctype.h> 4 #define MAXN 1000 5 char buf[MAXN]; 6 int main(){ 7 int len,j,sum; 8 char oldch; 9 while(fgets(buf,MAXN,stdin) != NULL){10 len = strlen(buf);11 sum = 0;12 oldch = buf[0];13 for(j = 1; j < len; j++){14 if(isalpha(oldch) > 0 && isalpha(buf[j]) == 0){15 sum++;16 }17 oldch = buf[j];18 }19 printf("%d\n",sum);20 }21 }
刚开始犯的错误为:认为开端一定为字符,判断就使用了isalpha(j) == 0 && isalpha(j-1) > 0,这导致开端为非字符的时候出现问题。
494 - Kindergarten Counting Game
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。