本人刚学C,做了一个题,大家帮忙帮忙看看这个代码
输入一行字符,统计其中有多少个单词,单词之间用空格分隔开。
#include<stdio.h>
#include<string.h>
int main(){
int count,word;
char *p,str[200];
p=str;
printf("请输入一行单词,单词间用空格隔开\n");
gets(str);
for(count=0;*p!='\0';p++)
if(*p==' '||*p=='\0')
count++;
if(str[0]=='\0') word=0;
else word=count+1;
printf("There are %d words in this line.",word);
return 0;
}
程序有误,循环体应包含两个if语句,需要用花括号括起来