fork download
  1. #include <stdio.h>
  2. int main()
  3. {
  4. char string[81];
  5. int i,num=0,word=0;
  6. char c;
  7. gets(string); //输入一个字符串给字符数组string
  8. for(i=0;(c=string[i])!='\0';i++) //只要字符不是'\0'就循环
  9. if(c==' ') word=0; //若是空格字符,使word置0
  10. else if(word==0) //如果不是空格字符且word原值为0
  11. { word=1; //使word置1
  12. num++; //num累加1,表示增加一个单词
  13. }
  14. printf("There are %d words in this line.\n",num); //输出单词数
  15. return 0;
  16. }
  17.  
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
There are 1 words in this line.