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

计算某字符出现次数

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

c++

#include<bits/stdc++.h>
using namespace std;

int main(){
    string s;
    //cin>>s;
    getline(cin,s); //注意可能有空格!
    char ch;
    cin>>ch;
    int res=0;
    for(auto &ss:s){
        if(isalpha(ch)){ //如果要查找的字符是字母
            if(ss==ch || ss+32==ch ||ss-32==ch){ //大、小写字母的ASCII码值相差32
              res++;
            }
        }
        else{
            if(ss==ch){
                res++;
            }
        }
    }
    
    cout<<res<<endl;
    return 0;
}
全部评论

相关推荐

数开小菜鸡:你是我今早见过的最美的牛客女孩......
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务