直接上代码 #include <stdio.h> #include <string.h> int wordscount(const char *s) { char *p = (char*)s; int count = 0; while (*p != '/0') { /*去掉空格*/ while (*p == ' ') { ++p; } /*单词计数*/ if (*p != '/0') { ++count; } /*跳去单词*/ while (*p != ' ' && *p != '/0') { ++p; } } return count; } int main() { char s[100]; printf("please input the strings:/n"); gets(s); printf("%d/n",wordscount(s)); return 0; }