题解 | #统计字符#C++ unordered_map实现

统计字符

https://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5

#include <iostream>
#include <unordered_map>
using namespace std;
void CountChar(string input) {
    int len = input.size();
    unordered_map<char, int> pair;
    for (int i = 0; i < len; i++) {
        if (isalpha(input[i])) {
            pair['C'] += 1;
        } else if (input[i] == ' ') {
            pair['B'] += 1;
        } else if (isdigit(input[i])) {
            pair['N'] += 1;
        } else {
            pair['O'] += 1;
        }
    }
    cout << pair['C'] << endl;
    cout << pair['B'] << endl;
    cout << pair['N'] << endl;
    cout << pair['O'] << endl;
}

int main() {
    string input;
    getline(cin, input);
    CountChar(input);

    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

不愿透露姓名的神秘牛友
03-29 08:32
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务