C++ 字符串字母统计:忽略大小写
#include
int main() { std::string str; std::cout << '请输入一个字符串:' << std::endl; std::getline(std::cin, str);
std::map<char, int> count_map;
for (auto& c : str)
{
if (std::isalpha(c)) // 判断是否为字母
{
c = std::tolower(c); // 转换为小写字母
++count_map[c]; // 字母计数
}
}
std::cout << '各个字母出现次数如下:' << std::endl;
for (auto& pair : count_map)
{
std::cout << pair.first << ': ' << pair.second << std::endl;
}
return 0;
}
原文地址: https://www.cveoy.top/t/topic/lHU0 著作权归作者所有。请勿转载和采集!