高质量题解|字符统计
2026-03-16 21:33:14
发布于:北京
0阅读
0回复
0点赞
题目大意
在给定的字符串中统计大写字母、小写字母、数字和其他字符的数量
考纲知识点
分支结构、ASCII 码、输入输出、字符串、基础数据类型、变量的定义以及使用
数据范围
一行字符,长度小于
解题思路
在循环中用 ASCII 判断字符属于大写字母、小写字母、数字还是其他字符
大写字母 ASCII :
小写字母 ASCII :
数字 ASCII :
参考程序
#include <bits/stdc++.h>
using namespace std;
int main(){
int a = 0,b = 0,c = 0,d = 0;
string s;
cin >> s;
for(int i = 0;i <= s.size()-1;i++){
if((int)s[i] >= 65 && (int)s[i] <= 90){
a++;
}else if((int)s[i] >= 97 && (int)s[i] <= 122){
b++;
}else if((int)s[i] >= 48 && (int)s[i] <= 57){
c++;
}else{
d++;
}
}
cout << a << endl;
cout << b << endl;
cout << c << endl;
cout << d << endl;
return 0;
}
时间复杂度
这里空空如也








有帮助,赞一个