[C] 统计单词数 →→→→→进入此内容的聊天室

来自 , 2021-01-27, 写在 C, 查看 149 次.
URL http://www.code666.cn/view/a3eb043e
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define MAX_STRING 200
  5.  
  6. int main(void) {
  7.         char str[MAX_STRING] = { 0 };
  8.         int i = 0;
  9.         int length = 0;
  10.         int count = 0;
  11.  
  12.         /* 打印原字符数组 */
  13.         puts("Input original String:");
  14.         gets(str);
  15.  
  16.         length = strlen(str);
  17.  
  18.         /* 统计单词个数 */
  19.         for (i = 0; i < length; ++i) {
  20.                 if (str[i] != ' ') { /* 单词开始 */
  21.                         ++count;
  22.                         while (' ' != str[i] && '\0' != str[i]) /* 单词结束 */
  23.                                 ++i;
  24.                 }
  25.         }
  26.  
  27.         printf("There are %d words in \"%s\".\n", count, str);
  28.  
  29.         return 0;
  30. }
  31.  

回复 "统计单词数"

这儿你可以回复上面这条便签

captcha