C代码--输入一句英文,判断单词的个数

来源:百度知道 编辑:UC知道 时间:2024/06/16 10:04:03
#include<stdio.h>
#include <string.h>
main()
{ int i,cnt=0,word;
char str[80];
printf("Input a string:");
gets(str);
strcat(str,' ');
for(i=0;str[i]!='\0';i++)
if(str[i]==' ')
{word=1;cnt++;}
else word=0;
printf("There is %d words!",cnt);
}
总是提示第8行有问题,不知道怎么搞的啊?请大家帮帮忙啊!
[Error] D:\My Documents\C-Free\Temp\未命名3.cpp:8: passing `char' to argument 2 of `strcat(char *, const char *)' lacks a cast

#include<stdio.h>
#include <string.h>
main()
{ int i,cnt=0,word;
char str[80];
printf("Input a string:");
gets(str);
strcat(str," ");
for(i=0;str[i]!='\0';i++)
if(str[i]==' ')
{word=1;cnt++;}
else word=0;
printf("There is %d words!",cnt);
}

只有一处有问题,strcat( , )参数是两个字符串。所以应为strcat(str," ")