Most frequent letter in string

Hi, I have a user inputted char array that I have then turned into a string, only letters. I have tried searching for a solution to find the most common character in a string, but I either don't understand, or it just didn't work. Therefore, I am trying to edit an example piece. The example piece finds the most common number in an array, I need the most common letter in a string. Example below:

1
2
3
4
5
6
7
8
9
10
11
 int i,max=0,mode=0;
	int fre[100]={0};//all values of array initialised with 0 
	for(i=0;i<SIZE;i++) 
		++fre[x[i]]; //count the occurence of each number 
	for(i=0;i<100;i++) //loop to find most frequent number 
	if(fre[i]>max)
	{
		max=fre[i];
		mode=i;
	}
	cout<<"The most frequent number is "<<mode<<" occuring "<< fre[mode]<<" times\n";


I have tried changing i from an int to a char, which sort of works, However, it will only work for 'a'. Regardless if there is a more frequent letter, it shows me the frequency of 'a'. Im not sure if I am on the right track, or if there is a better way to do this? Any help appreciated.

PS. I changed the inputted char array into a string, as I needed to return the letters, but was unable to work out how to do so with an array. Hence turned into a string, then returned.

Thanks
Crimson
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
#include <climits>
#include <cctype>

// prints one most frequent character in the string
void print_most_frequent_char_in( const std::string& str )
{
    // array to keep track of frequencies (one entry per possible character)
    int freq[ UCHAR_MAX + 1 ] {} ; // initialise to all zeroes

    int max_freq = 0 ;
    char max_freq_char = 0 ;

    for( unsigned char c : str ) // for each (unsigned) character in str
    {
        // c = std::toupper(c) ; // uncomment for case-insensitive

        if( std::isalpha(c) && ++freq[c] > max_freq ) // skip if non alphabetic
        {
            max_freq = freq[c] ;
            max_freq_char = char(c) ;
        }
    }

    std::cout << "string: '" << str
              << "' most frequent char: '" << max_freq_char
              << "' occurrs " << max_freq << " times.\n" ;
}

int main()
{
    for( std::string str : { "abracadabra", "to be or not to be",
                             "grey were the geese and green was the grazing.",
                             "truth: tautograms triumph, trumpeting trills." } )
     {
        print_most_frequent_char_in(str) ;
     }
}

http://coliru.stacked-crooked.com/a/ba58ec455c11aaaa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
#include <cctype>
#include <map>
using namespace std;

string mostFrequent( const string& text, int &mx )
{
   map<char,int> freq;
   for ( char c : text ) if ( isalpha( c ) ) freq[tolower(c)]++;
   mx = 0;
   for ( auto p : freq ) if ( p.second > mx ) mx = p.second;
   string result;
   for ( auto p : freq ) if ( p.second == mx ) result += p.first;
   return result;
}

int main()
{
   string test = "We are such stuff as dreams are made on";
   int mx;
   string modes = mostFrequent( test, mx );
   cout << test << '\n';
   cout << "Most common letter(s): " << modes << "( " << mx << " times )\n";
}


We are such stuff as dreams are made on
Most common letter(s): ae( 5 times )
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <algorithm>
#include <cctype>
using namespace std; 
  
int main(){ 
	string s= "Any strkkkingaa";	
	int ar[26]{0};	
	for(auto x: s){		
		ar[tolower(x)-int('a')]++;
	}	
	int n = *max_element(ar, ar+26);
	cout << "max appearance= "<< n << " times\n";
	for(int i=0; i<26; i++){
		if(ar[i]==n){
			cout << char(i+int('a')) << ", ";
		}
	}
	
return 0; 
} 
Thanks for the help, found the solution by anup, to be the most helpful.
Topic archived. No new replies allowed.