C++ 统计字符串中出现次数最多的小写字母
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
string str;
cin >> str;
vector<int> count(26, 0); // 用于统计每个小写字母的出现次数
for (char c : str) {
if (islower(c)) { // 判断是否为小写字母
count[c - 'a']++;
}
}
int maxCount = *max_element(count.begin(), count.end()); // 找到出现次数最多的小写字母的次数
char result = 'a';
for (int i = 1; i < 26; i++) {
if (count[i] == maxCount) {
result = max(result, (char)('a' + i)); // 找到ASCII码值最大的那个字母
}
}
cout << result << endl;
return 0;
}
原文地址: https://www.cveoy.top/t/topic/pawh 著作权归作者所有。请勿转载和采集!