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