#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    string s;
    cin >> s;
    
    vector<int> count(26, 0); // 初始化计数数组,用于统计每个字母出现的次数
    
    for (char c : s) {
        if (islower(c)) { // 只统计小写字母
            count[c - 'a']++; // 将字母出现的次数加1
        }
    }
    
    int maxCount = 0; // 记录出现次数最多的字母的次数
    char maxChar = 'a'; // 记录出现次数最多的字母的ASCII码值
    
    for (int i = 0; i < 26; i++) {
        if (count[i] > maxCount) { // 如果当前字母的出现次数大于maxCount,则更新maxCount和maxChar
            maxCount = count[i];
            maxChar = 'a' + i;
        } else if (count[i] == maxCount) { // 如果当前字母的出现次数和maxCount相等,则比较字母的ASCII码值
            maxChar = max(maxChar, char('a' + i)); // 更新maxChar为较大的字母
        }
    }
    
    cout << maxChar << endl;
    
    return 0;
}
C++ 找出字符串中出现次数最多的字母

原文地址: https://www.cveoy.top/t/topic/pawC 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录