以非字母类字符作为单词结束标志要考虑的情况稍多,且很容易出错,直接统计就好。
#include<cstdio> #include<cctype> int main() { char str[500]; while (gets(str) != NULL) { int cnt = 0,flag = 0; for (int i = 0; str[i]; i++) { if (isalpha(str[i])) { if (!flag) { flag = 1; cnt++; } else continue; } else flag = 0; } printf("%d\n",cnt); } return 0; }