统计字符串中单词的个数

#include <stdio.h>
#include <string.h>
#define SIZE 100
int main()
{
 char str[SIZE] = { '\0' };
 int count = 0;
 printf("please input the string\n");
 gets(str);
 puts(str);
 int length = strlen(str);//字符串的长度
 for (int i = 0; i < length; i++)
 {
  //判断是不是空格 不是的话在while里面i++执行判断到下一个空格的出现或是结束
  if (str[i] != ' ')
  {
   count++;
   while (str[i] != ' ' && str[i] != '\0')
   {
    i++;
   }
  }
 }
 printf("%d\n", count);
 return 0;
}

发布了39 篇原创文章 · 获赞 37 · 访问量 1997

猜你喜欢

转载自blog.csdn.net/weixin_43831728/article/details/97402928