program to display most repeatitive character in a sentence using array

there is one more issue if suppose to be there are two alphabets who occurs same time for example in a sentence :hello hey: h occurs 2 times e occurs 2 time and l occur 2 times then in result it is showing me 'e'.

// now couting and printing the most repetitive alphabet

for(int a=0; a<n; a++)
{
count[string[a]]++;
if( max<count[string[a]])
{
max=count[string[a]];
c=string[a];
}
}

cout<<" the most repetitive alphabet is: \t"<<c;
Last edited on
Never use gets. I sincerely hope you don't have an instructor who suggested its use. Since C11, gets has been removed from the C library.

main returns an int. This code should not compile on a modern compiler.

In C++, we try to keep keep variable scopes as small as possible. In for loops we try to limit the iterator variable to the scope of the function as you did with a in your last loop. Unfortunately, you did not do so for i in your first loop, so it is still in scope when you mistakenly use it inside your last loop.




I am using devc++ compiler. thanks for explaining.
can you able to help me with error as well?
If you're referring to the "error" that causes you to always get the last letter of the string, I thought I did. Use a instead of i in your last loop. (You might want to also keep in mind that it is counting spaces as well as letters.)
oh yes my mistake. thankyou for your help.
Last edited on
there is one more issue if suppose to be there are two alphabets who occurs same time for example in a sentence :hello hey: h occurs 2 times e occurs 2 time and l occur 2 times then in result it is showing me 'e'.
If you want your code to show all letters that have the highest frequency, you need to maintain a list of letters.

Pseudocode might be easier than explaining:

let z be an empty list of letters
let max be 0
let sequence be a series of letters
let count be an array of numbers all initialized to 0.

for each letter in sequence
    increment the count associated with letter
    if count is greater than max
        empty z
        add letter to z
        set max to count associated with letter
   else if count is equal to max
       add letter to z

display elements in z
Topic archived. No new replies allowed.