1133: 单词个数统计 C语言

1133: 单词个数统计
时间限制: 1 Sec 内存限制: 128 MB
提交: 11544 解决: 6533
[状态] [讨论版] [提交] [命题人:admin]
题目描述
从键盘输入一行字符,长度小于1000。统计其中单词的个数,各单词以空格分隔,且空格数可以是多个。

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

输出
单词的个数

样例输入 Copy
stable marriage problem Consists of Matching members
样例输出 Copy
7

#include <stdio.h>
#include <string.h> 


int main() {
    
    
	char str[1000];
	int i, len, count = 0;
	// 输入字符
	gets(str);
	
	// 统计单词个数 
	for(i = 0; str[i] != '\0'; i++){
    
    
		 if(str[i] != ' ' && str[i + 1] == ' ')
		 	count++;
	} 
	len = strlen(str);
	if(str[len - 1] != ' ')
		count++;
		
	printf("%d\n", count);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_45306379/article/details/121508057