UVA 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
#include<stdio.h>
#include<string.h>
int main()
{
    char str[1000];
    int k,i,word,num;
    while(gets(str) && str[0]!=EOF)
    {
        k=strlen(str);
        word=1;num=0;
        for(i=0;i<k;i++)
        {
            if((str[i]>='A' && str[i]<='Z')||(str[i]>='a' && str[i]<='z'))
            {
                if(word==1)
                {
                  num++;
                  word=0;
                }
            }
            else word=1;
        }
        printf("%d\n",num);
    }
    return 0;
}

 

 
#include<stdio.h>
int main()
{
    char ch;
    int word=1,count=0;
    while((ch=getchar())!=EOF)
    {
        if( (ch>='A' && ch<='Z') || (ch>='a' && ch<='z') )
        {
            if(word==1)
            {
                count++;
                word=0;
            }
        }
        else if(ch=='\n')
        {
            printf("%d\n",count);
            count=0;
            word=1;
        }
        else word=1;
    }
    return 0;

}

标记变量 单词出现时word==1, 否则word==0;

posted @ 2012-05-31 15:54  刘壮  阅读(836)  评论(0编辑  收藏  举报