蓝桥杯和ACM基础训练---[编程入门]字符串分类统计(C++)

题目描述
输入一行字符,分别统计出其中英文字母、数字、空格和其他字符的个数。

输入
一行字符

输出
统计值

样例输入
aklsjflj123 sadf918u324 asdf91u32oasdf/.’;123
样例输出
23 16 2 4

#include<iostream>
using namespace std;
int main()
{
	char ch;
	int count_letter = 0;
	int count_number = 0;
	int count_space = 0;
	int count_other = 0;

	cin.get(ch);
	while (ch != '\n'){
		if ((ch >= 'a'&&ch <= 'z') || (ch >= 'A'&&ch <= 'Z')) count_letter++;
		else if (ch >= '0' && ch <= '9') count_number++;
		else if (ch == ' ')count_space++;
		else count_other++;
		cin.get(ch);
	}
	cout << count_letter << " " << count_number << " " << count_space << " " << count_other;
	return 0;
}
发布了28 篇原创文章 · 获赞 6 · 访问量 2856

猜你喜欢

转载自blog.csdn.net/weixin_45621658/article/details/103437393