计算字符个数
题目描述
写出一个程序,接受一个由字母和数字组成的字符串,和一个字符,然后输出输入字符串中含有该字符的个数。不区分大小写。
输入描述:
输入一个有字母和数字以及空格组成的字符串,和一个字符。
输出描述:
输出输入字符串中含有该字符的个数。
题目的意思应该是:先输入一串字符(可能包含空格),然后回车,再输入一个字符,回车。
题里面并没有说明不区分大小写啊,提交之后才知道,既然区分,就都转成大写的吧。
#include <iostream>
#include <string>
#include <algorithm>//transform()
using namespace std;
int main()
{
string str;
string single;
int count=0;
getline(cin,str);
transform(str.begin(), str.end(), str.begin(), ::toupper);//转成大写
getline(cin,single);
transform(single.begin(), single.end(), single.begin(), ::toupper);
for(int i=0;i<str.length();i++)
{
if(str[i]==single[0])
{
count++;
}
}
cout << count;
return 0;
}