思路:
1、题目要求,字符串长度小于500,需先设置字符串长度限制
2、将第一行的字符串输入
3、将待检测的字母出入
4、将待检测字母与输入字符串比较,如果相同count+1,最后输count
注意:在比较的过程中
方法一、if(str[i] == s||str[i] == s - 32 || str[i] == s + 32)
因为不确定待检测字母的大小写,所以将三种形式都考虑在内。
方法二、if(tolower(str[i] )== tolower(s)
由于不区分大小写,所以可将字符串都转化为小写字母,再统一进行比较。
在C++语言中
tolower()函数是把字符串都转化为小写字母
touppre()函数是把字符串都转化为大写字母
#include <iostream>
#include <string>
using namespace std;
const int N = 500;
string str[N];
int main()
{
string str;
getline(cin,str);
char s;
cin >> s;
int count=0;
for(int i= 0 ;str[i] !='\0';i++)
{
//if(str[i] == tolower(s)||str[i] == tolower(s) - 32)
if(str[i] == s||str[i] == s - 32 || str[i] == s + 32)
{
count++;
}
}
cout << count << endl;
return 0;
}