zrq495
www.zrq495.com

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


题义:判断单词的个数。水题。


代码如下:


 1 #include<stdio.h>
 2 #include<string.h>
 3 
 4 int main()
 5 {
 6     int n, i, num, word;
 7     char str[1000];
 8     while(gets(str))
 9     {
10         num=0;
11         word=1;
12         n=strlen(str);
13         for (i=0; i<n; i++)
14         {
15             if ((str[i] >= 'a' && str[i] <= 'z') || (str[i]>='A' && str[i] <= 'Z'))
16             {
17                 if (word)
18                 {
19                     num++;
20                 }
21                 word=0;
22             }
23             else
24             {
25                 word=1;
26             }
27         }
28         printf("%d\n", num);
29     }
30     return 0;
31 }
posted on 2012-05-20 11:16  zrq495  阅读(195)  评论(0编辑  收藏  举报