这道c语言题哪里错了?

来源:百度知道 编辑:UC知道 时间:2024/05/14 13:27:01
#include"stdio.h"
#define SIZE 100
void tongji_output(char string[]);
main()
{
char string[SIZE];
gets(string);
tongji_output(string);
}
void tongji_output(char string[])
{
int i,number=0,word=0;
char c;
for(i=0;(c=string[i])!='\0';i++)
if(c=='') word==0;
else if(word==0)
{
word=1;number++;
}
printf("there are %d words in the line\n",number);
}

char c; 改成char c='';否则if(c=='') word==0; 会出错

for(i=0;(c=string[i])!='\0';i++)
if(c=='') word==0;
else if(word==0)
{
word=1;number++;
}

你想清楚点再写出来,表达的清楚点
建议改成
void tongji_output(char string[])
{
int i,num=0;
for(i=0;string[i]!='\0';i++)
{
if(string[i]!=' '&&(string[i+1]==' '||string[i+1]=='\0'))
num++;
}
printf("there are %d words in the line\n",num);
}

楼上说的好象是对的