题目链接:
注意点:
1.输入:getline()和getchar(), 其中getline()即使遇到空格读入也不会结束,getchar()函数的作用是从标准的输入stdin中读取字符。也就是说,getchar()函数每次只读取一个字符。
2.大小写转换:toupper()和tolower()
3. 循环遍历:for(auto a:b),a遍历并获得b容器中的每一个值,但a无法影响到b容器中的元素。for(auto &a:b)可以对容器中的内容进行赋值,通过a赋值来做到容器b的内容填充。
#include <iostream>
#include <cstring>
#include <ctype.h>
using namespace std;
int main() {
string str;
getline(cin,str);
//转小写也是可以的
char target = toupper(getchar());
int cnt = 0;
for( auto i : str ){
if (toupper(i) == target) cnt++;
}
cout << cnt<< endl;
return 0;
}