统计单词(选作)

| October 23, 2007 15:08 | timmy | Via Original
输入一个正整数repeat (0输入一行字符,统计其中单词的个数。各单词之间用空格分隔,空格数可以是多个。
例:括号内是说明
输入
2   (repeat=2)
Reold building  room   123
 Programming is fun
输出
4
3


#include <stdio.h>
int main( )
{
  int ri, repeat;
  int count, word;
  char c;

  scanf("%d", &repeat);
  getchar();
  for(ri=1; ri<=repeat; ri++){
    c = getchar();
   word=0;count=1;
   while(c!='\n'){
    if(c!=' ')    word=1;
    if(c==' '&&word==1){  
     count++;
     word=0;
    }
     c=getchar();
 
   }
   if(word==0) count--;
      printf("%d\n", count);
  }
}
Tags:
Program/Code » C/C++ | Comments(0) | Trackbacks(0) | Reads(288)
Add a comment
 Site URI
 Email
  Password Optional
 Nickname  *  [Register]
               

 
Emots
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
Enable HTML
Enable UBB
Enable Emots
Hidden
Remember