供参考:
#include<stdio.h>
#include<string.h>
int fun(char* s)
{
int cnt = 0, flg = 1;
char* p = s;
while (*p) {
if (*p == ' ' || *p == '\0') flg = 1;
else if (flg) {
cnt++;
flg = 0;
}
p++;
}
return cnt;
}
int main()
{
char s[512];
int cnt = 0;
gets(s);
cnt = fun(s);
printf("%d", cnt);
return 0;
}
可以试试以空格作为判断依据,单词数比空格数多一个