接受一个有字母和数字以及空格组成的字符串,和一个字符,然后输出输入字符串中含有该字符的个数。不区分大小写
#include<iostream> #include<sstream> using namespace std; int main() { string str; while (getline(cin, str)) { char ch; string str2; getline(cin,str2); ch=str2[0]; char ch2 = 0; if (ch >= 'a'&&ch <= 'z') { ch2 = ch - 32; } if (ch >= 'A'&&ch <= 'Z') { ch2 = ch + 32; } int count = 0; for (int i = 0; i<str.size(); i++) { if (str[i] == ch || str[i] == ch2) { count++; } } cout << count << endl; } return 0; } </sstream></iostream>