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

计算某字符出现次数

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

#include <iostream>
#include <string>
#include <cctype> 
#include <algorithm>
using namespace std;
int ch_of_cnt(const string& str,const char& ch)
{
    int cnt = 0;
    for (char c : str) {    
        if (isalpha(c) && tolower(c) ==tolower(ch)) {
                cnt++;
        } else if (c == ch) {
                cnt++;
        }
    }
    return cnt;
}

int main() {
    string str1,str2 ;
    getline(cin,str1); 
    getline(cin,str2);
    char ch = str2.front();
    cout << ch_of_cnt(str1,ch) <<endl;
}

// 64 位输出请用 printf("%lld")

全部评论

相关推荐

哥_留个offer先:跟他说,你这个最好用c#,微软就用c#Java不适合这个项目
点赞 评论 收藏
分享
05-09 13:22
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务