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;

相关文章:

  • 2021-11-09
  • 2021-10-01
  • 2021-12-27
  • 2021-07-21
  • 2022-12-23
  • 2021-07-23
  • 2022-12-23
  • 2021-06-22
猜你喜欢
  • 2021-11-16
  • 2021-10-28
  • 2022-12-23
  • 2021-08-28
  • 2021-09-07
  • 2021-10-10
  • 2021-05-23
相关资源
相似解决方案