苹果手机文件在哪打开:C语言编程高手给看看这个程序错在哪里

来源:百度文库 编辑:中科新闻网 时间:2024/05/03 11:27:16
#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);
}

这个程序的要求是输入一段 英文,然后判断一共有几个单词,为什么我输入 I am a boy. 回车后, 出现
There are 1 words in the line.怎么会只有一个,程序是我从书上找的,不应该错啊,如果错了,请大家给改正一下!

if(c=='')word=0;
改成
if(c==' ')word=0;
加个空格就行了。
这个程序现在我输入 I am a boy. 回车后, 出现
There are 3 words in the line.

1.应该养成良好的习惯 把for所管辖的语句用{}括起来
2. 问题在 if(c=='')word=0;
两个''之间应该有一个空格ok?而不是空的,你试试改过来看看

你的for后面没有
{
...
}
?