题目描述
输入一行字符串,计算其中A-Z大写字母出现的次数
输入描述:
案例可能有多组,每个案例输入为一行字符串。
输出描述:
对每个案例按A-Z的顺序输出其中大写字母出现的次数。
示例1
输入
DFJEIWFNQLEF0395823048+_+JDLSFJDLSJFKK
输出
A:0
B:0
C:0
D:3
E:2
F:5
G:0
H:0
I:1
J:4
K:2
L:3
M:0
N:1
O:0
P:0
Q:1
R:0
S:2
T:0
U:0
V:0
W:1
X:0
Y:0
Z:0
题目解析:使用map计数,并且26个字母如果没有也需要统计
代码:
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<iomanip>
#include<vector>
#include<map>
#include<stack>
#include<queue>
using namespace std;
int main()
{
string sentence;
map<char,int> mp;
while(cin >> sentence){
char zimu = 'A';
for(int i = 0; i < 26; i++){ //因为需要显示26个字母,不管出没出现,所以提前设置全部为0
zimu++;
}
for(int i = 0;i < sentence.size(); i++){
if(sentence[i] >= 'A' && sentence[i] <= 'Z'){
mp[sentence[i]]++;
}
}
map<char,int>::iterator it = mp.begin();
for(it;it != mp.end(); it++){
cout << it->first << ":" << it->second << endl;
}
mp.clear();
}
return 0;
}