1. 程式人生 > >10月7日 c語言 輸入一串字元,單詞之間用空格隔開,統計其中有多少個單詞

10月7日 c語言 輸入一串字元,單詞之間用空格隔開,統計其中有多少個單詞

#include<stdio.h>
int main()
{
char string[100];
int i,num=0,word=0;
char c;
printf("please input a sentense\n");
gets (string);
for(i=0;(c=string[i])!='\0';i++)
if(c==' ')word=0;
else if(word==0)
{
word=1;
num++;
}
printf("there are %d words in this line.\n",num);
return 0;
}