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

计算某字符出现次数

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

#include <iostream>
#include <cstring>
using namespace std;

const int N = 40; // 26+10+1=37
int cnt[N];

int main()
{
    string str;
    getline(cin, str);
    for (auto c : str)
    {
        if (c >= 'a' && c <= 'z')
            cnt[c - 'a']++;
        else if (c >= 'A' && c <= 'Z')
            cnt[c - 'A']++;
        else if (c >= '0' && c <= '9')
            cnt[c - '0' + 26]++;
        else if (c == ' ')
            cnt[37]++;
    }

    char t;
    cin >> t;
    if (t >= 'a' && t <= 'z')
        cout << cnt[t - 'a'] << endl;
    else if (t >= 'A' && t <= 'Z')
        cout << cnt[t - 'A'] << endl;
    else if (t >= '0' && t <= '9')
        cout << cnt[t - '0' + 26] << endl;
    else if (t == ' ')
        cout << cnt[37] << endl;

    return 0;
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务