题解 | 计算某字符出现次数

计算某字符出现次数

https://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1

#include <iostream>
#include <unordered_map>
#include <string>
using namespace std;

int main() {
    string str;
    getline(cin,str);
    unordered_map<char,int>mp;
    for(char i : str){
        mp[i]++;
    }
    char target;
    cin >> target;
    int ans = 0;
    if( target >='a' && target <= 'z'){
        ans = mp[target] + mp[target  - 32];
    }else if (target >='A' && target <= 'Z'){
        ans = mp[target] + mp[target  + 32];
    }else {
        ans = mp[target];
    }
    cout << ans <<endl;
    return 0;

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

主要是,ascii表中的大写字母范围和小写字母范围,大写的A是65 ,小写的a是97;两者之间隔着32;

全部评论

相关推荐

10-17 23:18
已编辑
西北农林科技大学 Web前端
独行m:给25可以试试,但他只能给12,那就是纯纯的事精
秋招,不懂就问
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务