用c++编写程序,输入一个字符串,统计其中各个 字母出现的次数,忽略大小写。
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
#include <cctype>
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/vmA 著作权归作者所有。请勿转载和采集!