题解 | #明明的随机数#
计算某字符出现次数
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;
}
