题解 | #计算某字符出现次数#
计算某字符出现次数
https://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1
// HJ2 计算某字符出现次数.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
class CharTimes {
public:
void Times(string& str, string& s);
};
void CharTimes::Times(string& str, string& s)
{
for (int i = 0; i < str.size(); i++)
{
if (islower(str[i]))
{
str[i] = toupper(str[i]);
}
}
if (islower(s[0]))
s[0] = toupper(s[0]);
int ans = 0;
for (auto c : str)
{
if (s[0] == c)
ans++;
}
cout << ans << endl;
}
int main()
{
CharTimes a;
string str, s;
getline(cin, str);
cin >> s;
a.Times(str, s);
return 0;
}
查看25道真题和解析