C++ 统计字符串中只出现一次的字符的个数

#include "iostream"
#include "string"
using namespace std;

//统计str中字符出现一次的个数
int countstring(string str){
// hash散列
int hash[258]={0};
// 散列出现的字符
for (int i = 0; i < str.size(); ++i) {
hash[str[i]]++;
}
// 统计出现一次的字符个数
int sum=0;
for (int i = 0; i < 258; ++i) {
// 如果i这种字符只出现了一次,统计一下
if(hash[i]==1){
sum++;
}
}
return sum;
}
int main(){
string str;
cin>>str;
cout<<countstring(str);
return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_53064820/article/details/130644859
今日推荐