循环-06. 统计一行文本的单词个数

循环-06. 统计一行文本的单词个数(15)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
张彤彧(浙江大学)

本题目要求编写程序统计一行字符中单词的个数。所谓“单词”是指连续不含空格的字符串,各单词之间用空格分隔,空格数可以是多个。

输入格式:

输入给出一行字符。

输出格式:

在一行中输出单词个数。

输入样例:
Let's go to room 209.
输出样例:
5
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<math.h>
 4 int main()
 5 {
 6     char str[2];
 7     char a;
 8     int count = 0;
 9     int i = 0;
10     while(1)
11     {
12         scanf("%c", &a);
13         str[i % 2] = a;
14         if(i > 0 && str[i%2] == ' ' && str[(i-1)%2] != ' ')
15             count++;
16         i++;
17         if(a == '\n')
18             break;
19     }
20     if(str[(i-2)%2] == ' ')
21         printf("%d\n", count);
22     else
23         printf("%d\n", count + 1);
24     return 0;
25 }

 

posted @ 2015-01-13 22:47  Yomman  阅读(1240)  评论(0编辑  收藏  举报