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

计算某字母出现次数

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

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string str;
    char input;
    int count = 0;
    getline(cin, str);
    cin >> input;
    const char* s = &str[0];
    while (*s!=0)
    {
        if (*s == input||*s+32==input||*s-32==input)
        count++;
        s++;
    }
    cout << count << endl;
}

使用getline获取含有空格的字符串,使用指针判断字符串中字符如输入是否一致,关键在于大小写的合法判断,已知大小写字母的as码相差32,char类型比较的本质是在比较as码。

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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