题解 | #明明的随机数#

计算某字符出现次数

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

这里应该要考虑三种情况了:
1.统计的是数字;
2.统计的是字母;
3.统计空格;
所以输入的字符需要考虑空格的情况。
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str;
    string c;

    getline(cin, str);
    getline(cin, c);

    int k = 0;

    if(c[0] > 64)
    {
        for(int i = 0; i < str.length(); i++)
        {
            if(str[i] == c[0] || str[i]+32 == c[0] || str[i]-32 == c[0])
            {
                k++;
            }
        }
    }
    else
    {
        for(int i = 0; i < str.length(); i++)
        {
            if(str[i] == c[0])
            {
                k++;
            }
        }
    }

    cout << k << endl;

    return 0;
}

全部评论

相关推荐

重生我想学测开:嵌入式的问题,我准备入行京东外卖了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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