c语言,输出的值不清楚。哪位仁兄,来看看,帮我解解难。

来源:百度知道 编辑:UC知道 时间:2024/06/05 00:57:13
#include<stdio.h>
main()
{
char string[81];
int i,num=0,word=0;
char c;
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 the line.\n",num);
getch();
}
当我运行时。编译是成功的。但是当我输入,I am a boy .时,结果只出现There are 1 words im the line.但为什么不是4个单词啊!究竟哪里出错啦!?

if(c=='')改为if(c==' ')你空格打错了,另外把最后面的getch();去掉,那个起不了什么作用,你前面已经有gets();。

哥们儿,你的判断逻辑有问题,只要统计空格符号的数目+1就是单词数目。
main()
{
char string[81];
int i,num=0,word=0;
char c;
gets(string);
for (i=0;(c=string[i])!='\0';i++) {
printf("%c\n",c);
if (c==' '){
printf("here\n");
num++;
}

}
num++;
printf ("There are %d words in the line.\n",num);

}