#includevoid main(){char string[81];int i,num=0;word=0;char c;gets(string[i];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)}
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/18 04:23:49
#includevoid main(){char string[81];int i,num=0;word=0;char c;gets(string[i];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)}
#include
void main()
{
char string[81];
int i,num=0;word=0;
char c;
gets(string[i];
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)
}
#includevoid main(){char string[81];int i,num=0;word=0;char c;gets(string[i];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)}
你的程序都有错误的说
#include
void main()
{
char string[81];
int i,num=0,word=0;
char c;
gets(string); /*这里必须用string,不能用string[i]*/
for(i=0;(c=string[i])!='\0';i++) /*假如字符串没有结尾,则继续判断*/
if(c==' ') word=0; /*如果为空格的话,将word赋值为0*/
else if(word==0) /*如果上一次判断时,c为空格,则word为0,此时将word赋值为1,num加1*/
{
word=1; /*意思就是,如果上一次遇到了空格,证明一个单词结束,num计数加1(这里有问题的,这个程序其实不太完美)*/
num++;
}
printf("There are %d words in the line.\n",num);
}