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

计算某字符出现次数

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

方法一:不用哈希,模拟一下可以A

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string str;
    getline(cin,str);//带着空格一起读入
    char c;
    cin>>c;
    int i;
    int t=0;
    for(i=0;i<str.size();i++)
    {
        if(c>='a'&&c<='z')//给的字符是小写字母的情况
        {
            if(str[i]==c||str[i]==c-32)
                t++;
        }
        else if (c>='A'&&c<='Z')//给的字符是大写字母的情况
        {
            if(str[i]==c||str[i]==c+32)
                t++;
        }
        else if(str[i]==c)//给的字符是数字的情况
            t++;
    }
    cout<<t;
    return 0;
}
全部评论

相关推荐

昨天 11:21
复旦大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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