题解 | #计算某字母出现次数#
计算某字母出现次数
http://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1
从头开始遍历
找到数入字母对应的大小写,则加一;否则,遍历下一个字符
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
int len, flag = 0;
getline(cin, str);
char c;
c = getchar();
len = str.size();
for (int i = 0; i < len; i++)
{
if (c == str[i] || c == str[i] - 32 || c == str[i] + 32)
flag++;
continue;
}
cout << flag << endl;
return 0;
}
#include<string>
using namespace std;
int main()
{
string str;
int len, flag = 0;
getline(cin, str);
char c;
c = getchar();
len = str.size();
for (int i = 0; i < len; i++)
{
if (c == str[i] || c == str[i] - 32 || c == str[i] + 32)
flag++;
continue;
}
cout << flag << endl;
return 0;
}