C++ counts the number of characters that appear only once in a string

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

// count the number of characters in str
int countstring(string str){ // hash hash int hash[258]={0}; // hash List the characters that appear for (int i = 0; i < str.size(); ++i) { hash[str[i]]++; } // Count the number of characters that appear once int sum=0; for (int i = 0; i < 258; ++i) { // If the character i appears only once, count if(hash[i]==1){ sum++; } } return sum; } int main (){ string str; cin>>str; cout<<countstring(str); return 0; }





















Guess you like

Origin blog.csdn.net/weixin_53064820/article/details/130644859