输入一行字符,统计其中有多少个单词,单词之间用空格分隔开
#include<stdio.h>
int main()
{
char str[81];
int number=0, i, word=0;
gets(str);
for(i=0;(str[i])!='\0';i++)
if(str[i]=="") word=0;
else if(word==0)
{
word = 1;
number += 1;
}
printf("%d", number);
return 0;
}
程序报错如下:
1.error C2446: '’ : no conversion from ‘char *’ to ‘int’ This conversion requires a reinterpret_cast, a C-style cast or function-style cast
2.error C2040: '’ : ‘int’ differs in levels of indirection from ‘char [1]’
检查错误发现,单个字符应该是单引号,而不能用双引号。
#include<stdio.h>
int main()
{
char str[81];
int number=0, i, word=0;
gets(str);
for(i=0;(str[i])!='\0';i++)
if(str[i]==' ') word=0;
else if(word==0)
{
word = 1;
number += 1;
}
printf("%d", number);
return 0;
}
结果为: