【002-计算字符个数】
https://blog.csdn.net/DERRANTCM/article/details/51231912
C++实现:
void main()
{
string inStr;
getline(cin,inStr);
char ch;
cin >> ch;
string::size_type chNum = inStr.size();
transform(inStr.begin(),inStr.end(),inStr.begin(),toupper);
ch = toupper(ch);
int n=0;
for (string::size_type i=0;i<chNum;i++)
{
if(ch == inStr[i])
n++;
}
cout << n;
system("pause");
}
方法2:
void main()
{
string strIn,strOut;
getline(cin,strIn);
char chIn;
cin >> chIn;
chIn = toupper(chIn);
char ch;
int sum = 0;
for (int i=0; i<strIn.size();i++)
{
ch = toupper(strIn.at(i));
strOut.push_back(ch);
if(ch == chIn)
sum++;
}
cout << sum << endl;
cout << strOut;
system("pause");
}