Ch Fr 1 2 3 4 5 6 ------------------- a 6 a a a a a a e 3 e e e o 4 o o o o
123456789101112131415161718192021222324252627282930
#include <iostream> #include <string> #include <algorithm> #include <cctype> #include <iomanip> // print the counts of occurrences of char ch in str void print_counts( const std::string& str, char ch ) { // https://en.cppreference.com/w/cpp/algorithm/count const auto n = std::count( str.begin(), str.end(), ch ) ; if( n > 0 ) // note: std::string( n, ch ) - a string of size n, where each character is ch std::cout << ch << std::setw(4) << n << " " << std::string( n, ch ) << '\n' ; } int main() { const std::string vowels = "aeiou" ; std::string str ; std::getline( std::cin, str ) ; std::cout << str << "\n\n" ; // range based loop: http://www.stroustrup.com/C++11FAQ.html#for for( char& c : str ) c = std::tolower(c) ; // convert to all lower case // print the histogram of vowels for( char v : vowels ) print_counts( str, v ) ; }