题解 | #计算某字母出现次数#
计算某字母出现次数
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码。
查看38道真题和解析