text: Print characters and their frequencies in order of occurrence | C ++ implementation to print the character and its frequency in order of its occurrence ; function to print the character and its frequency in order of its occurrence ; size of the string ' str ' ; ' freq [ ] ' implemented as hash table ; initialize all elements of freq [ ] to 0 ; accumulate frequency of each character in ' str ' ; traverse ' str ' from left to right ; if frequency of character str [ i ] is not equal to 0 ; print the character along with its frequency ; update frequency of str [ i ] to 0 so that the same character is not printed again ; Driver program to test above
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; #define SIZE  26 NEW_LINE void printCharWithFreq ( string str ) { int n = str . size ( ) ; int freq [ SIZE ] ; memset ( freq , 0 , sizeof ( freq ) ) ; for ( int i = 0 ; i < n ; i ++ ) freq [ str [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq [ str [ i ] - ' a ' ] != 0 ) { cout << str [ i ] << freq [ str [ i ] - ' a ' ] << " ▁ " ; freq [ str [ i ] - ' a ' ] = 0 ; } } } int main ( ) { string str = " geeksforgeeks " ; printCharWithFreq ( str ) ; return 0 ; }