写出一个程序,接受一个有字母和数字以及空格组成的字符串,和一个字符,然后输出输入字符串中含有该字符的个数。不区分大小写。
#include<iostream>
#include<string>
using namespace std;
int main() {
string input;
char target;
int count = 0;
getline(cin, input);
cin >> target;
for (int i = 0; i < input.length(); i++) {
if (input[i] == target)
count++;
}
cout << count <<endl;
system("pause");
return 0;
}
小结啦~
觉得程序里最传神的就是这一句啦:input[i] == target
简直就是字符串和字符的无缝对接好吗。