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

 

2
7
10
9



注意这里有一些问题,单独的标点符号不算单词,单词中一定有至少一个字母,而且以标点分割的字母算两个单词,比如jk....lakf 这算两个单词!
看到这里,是不是想到些什么呢?
对,状态量的标准是是不是字母,这样想来,这道题就简单多了!

#include "stdio.h"
#include "cstring"
#include "ctype.h"

int main ()
{
char temp[1000];

while(gets(temp)!=NULL)
{


int i,con = 0,count = 0;
for (i=0;i<=strlen(temp);i++)
{
if (!con&&isalpha(temp[i]))
{
count++;con = 1;
}

if (!isalpha(temp[i]))
con = 0;
}
printf("%d\n",count);

}
}

posted @ 2014-01-15 14:26  dxywx  阅读(272)  评论(0编辑  收藏  举报