count the number of occurrences of a character in a string algorithm code example
Example 1: count occurrences of character in string c++
std::string s = "a_b_c";
size_t n = std::count(s.begin(), s.end(), '_'); // n=2
Example 2: count occurrences of character in string java 8
String someString = "elephant";
long count = someString.chars().filter(ch -> ch == 'e').count();
assertEquals(2, count);
long count2 = someString.codePoints().filter(ch -> ch == 'e').count();
assertEquals(2, count2);