Q494: 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<stdlib.h>
int main()
{
    char string[100];
    char c;
    int i,word=0,num=0;
    gets(string);
      for(i=0;(c=string[i])!='\0';i++)
       {
       if(c==' ')  word=0;
       else if(word==0)
          {word=1;
          num++;}
       }
    printf("%d\n",num);
    }
首先思路就是每个单词之间都有空格隔开,所以从空格入手,最后以'\0'收尾。
                    
                
                
            
        
浙公网安备 33010602011771号