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

计算某字符出现次数

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

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

int main() {
    string s;
    getline(cin,s); //输入的字符串
    // cout << s << endl;
    char c;
    cin>>c;     //输入的字符
    int a = (c-'A');  //解决大小写的问题
    int res=0;
    if(a<0){
        a = c -'0';
        for(auto sc:s){
            if((sc-'0')==a){
                res++;
            }
    }
    }
    else{
        a = (c-'A')%32;  //解决大小写的问题
        // cout<<a<<endl;
        for(auto sc:s){
            if((sc-'A')%32==a){
                res++;
            }
    }
    }
    cout<<res<<endl;
    
}
// 64 位输出请用 printf("%lld")

是否区分别大小写以及是否区分字符和数字,都用ascii码转为整数

全部评论

相关推荐

不知道怎么取名字_:玩游戏都写到简历上了啊
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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