给出一个string字符串,统计里面出现的字符个数

时间:2023-03-09 06:06:49
给出一个string字符串,统计里面出现的字符个数

给出一个string字符串,统计里面出现的字符个数

解决方案:

使用algorithm里面的count函数,使用方法是count(begin,end,'c'),其中begin指的是起始地址,end指的是结束地址,第三个参数指的是需要查找的字符.


#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;char c;
cin>>s>>c;
int num=count(s.begin(),s.end(),c);
cout<<"在字符串"<<s<<"中"<<"字符c出现的次数"<<num<<endl;
}