494 - 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.
算一算每行有几个字(word)。
Word的定义是连续的字元(letter: A~Z a~z)所组成的字。
Input
测试资料每笔一行,每行至少有一个字。
Output
输出每一行的word数。
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> int main() {int n,i,sum; char a[100000]; while(gets(a)){for(i=1,n=0;a[i];i++){ if((a[i]<'A'||a[i]>'Z')&&(a[i]<'a'||a[i]>'z')) {if((a[i-1]>='A'&&a[i-1]<='Z')||(a[i-1]>='a'&&a[i-1]<='z')) n++;} } printf("%d\n",n); } return 0; }
注意:一个单词的定义