Return maximum occurring character in an input string | C ++ program to output the maximum occurring character in a string ; Create array to keep the count of individual characters and initialize the array as 0 ; Construct character count array from the input string . ; Initialize max count ; Initialize result ; Traversing through the string and maintaining the count of each character ; Driver program to test the above function
#include <bits/stdc++.h> NEW_LINE #define ASCII_SIZE  256 NEW_LINE using namespace std ; char getMaxOccuringChar ( char * str ) { int count [ ASCII_SIZE ] = { 0 } ; int len = strlen ( str ) ; int max = 0 ; char result ; for ( int i = 0 ; i < len ; i ++ ) { count [ str [ i ] ] ++ ; if ( max < count [ str [ i ] ] ) { max = count [ str [ i ] ] ; result = str [ i ] ; } } return result ; } int main ( ) { char str [ ] = " sample ▁ string " ; cout << " Max ▁ occurring ▁ character ▁ is ▁ " << getMaxOccuringChar ( str ) ; }