碰到一个c语言的问题
#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);
}
在viual c 中会查到一个错误 但我查了半天都没查出
还有哪位大哥帮我讲解以下数组吗 我好像对数组不是很了解。
再次谢谢各位!
----------------解决方案--------------------------------------------------------
for里面有问题
----------------解决方案--------------------------------------------------------
for (i=0,(c=string[i])!='\0';i++)
错误
应该有两个";"
改为for (i=0;(c=string[i])!='\0';i++)
----------------解决方案--------------------------------------------------------
对啊 谢谢 不过谁能帮我讲解一下数组呢
----------------解决方案--------------------------------------------------------
还是有错误
Compiling...
例8.cpp
H:\办公软件\VC6.0\MSDev98\MyProjects\project1\例8.cpp(8) : error C2018: unknown character '0xa3'
H:\办公软件\VC6.0\MSDev98\MyProjects\project1\例8.cpp(8) : error C2018: unknown character '0xbb'
H:\办公软件\VC6.0\MSDev98\MyProjects\project1\例8.cpp(8) : error C2143: syntax error : missing ';' before ')'
H:\办公软件\VC6.0\MSDev98\MyProjects\project1\例8.cpp(9) : error C2137: empty character constant
H:\办公软件\VC6.0\MSDev98\MyProjects\project1\例8.cpp(16) : warning C4508: 'main' : function should return a value; 'void' return type assumed
执行 cl.exe 时出错.
----------------解决方案--------------------------------------------------------
程序该为#include <stdio.h>
void 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
{
word=1;
num++;
}
printf("There are %d words in the line.\n",num);
}就OK了
----------------解决方案--------------------------------------------------------
for (i=0,(c=string[i])!='\0';i++)
if (c=='') word=0;
还有这个if语句中c == ' ' 中间有空格。
----------------解决方案--------------------------------------------------------
谢谢 管用
但是在运行中出现了一个问题
我输入了“I am a boy" 他说有7个word 这明显是4个词怎么会是7个呢
不过我在想想 知道的也能告诉我 谢谢
----------------解决方案--------------------------------------------------------
#include <stdio.h>
main()
{
char string[81];
int i,num=1,word=0;
char c;
gets (string);
for (i=0;(c=string[i])!='\0';i++)
{
if (c==' ')
{
word=1;
num+=word;
}
else
word=0;
}
printf("There are %d words in the line.\n",num);
}
你试试..应该可以了..但这个代码有好多bug...有待改进...
----------------解决方案--------------------------------------------------------
if (c==' ') word=0;
else if(word==0)
{
word=1;
num++;
你这样写意思是当遇到空格时不加的,当然是算有几个字母拉.而不是几个单词拉.
----------------解决方案--------------------------------------------------------