#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
int i,num=0,word=0;<span style="color:#ff0000;">//num为单词数 word作为判别当前是否开始了一个新单词的标志</span>
char c;
gets(str);
for(i=0;(c=str[i])!='\0';i++) <span style="color:#ff0000;">//只要字符不是‘\0’,就继续执行循环</span>
{
if(c==' ') <span style="color:#ff0000;">//如果是空字符,使word置0</span>
word=0;
else if(word==0) <span style="color:#ff0000;">//如果不是空格字符且word原值为0</span>
{
word=1; <span style="color:#ff0000;"> //使word置1</span>
num++; <span style="color:#ff0000;"> //num累加1,表示增加一个单词</span>
}
}
printf("%d\n",num);
return 0;
}
<img src="https://img-blog.csdn.net/20150413204751588?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMjczODEzNTE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />