zzulioj1133(单词个数)

题目描述
从键盘输入一行字符,长度小于1000。统计其中单词的个数,各单词以空格分隔,且空格数可以是多个。

输入
输入只有一行句子。仅有空格和英文字母构成

输出
单词的个数

样例输入
stable marriage problem Consists of Matching members

样例输出
7

#include<stdio.h>
 
int main(){
	char str[1000];
	int count = 0;
	gets(str);
	if (str[0]!=' ')
	{
		count++;//第一个单词
	}
	for (int i= 1; str[i]!='\0'; i++)
	{
		if (str[i-1] == ' '&&str[i] != ' '){//空格+字母的组合确定下一个单词
			count++;
		}
	}
	printf("%d\n",count);
 
	
}
发布了16 篇原创文章 · 获赞 0 · 访问量 339

猜你喜欢

转载自blog.csdn.net/m0_46238735/article/details/104388004