Answers for "occurrence of character in string in c++"

C++
3

count occurrences of character in string c++

std::string s = "a_b_c";
size_t n = std::count(s.begin(), s.end(), '_'); // n=2
Posted by: Guest on September-14-2020
0

lists occurrences of characters in the string c++

int d[27]={0};
for (int i = 0; i < str.length(); i++)	  //AABBCC
	d[str[i] - 'A']++;
for (int i = 0; i < str.length(); i++) 
	if (d[str[i] - 'A'] != 0) {
		cout << str[i] << " "<< d[str[i] - 'A'] << " "; //A 2 B 2 C 2 
}
Posted by: Guest on March-28-2021

Code answers related to "occurrence of character in string in c++"

Browse Popular Code Answers by Language