2
//Program to find max occurring character in string

#include<stdio.h>
#include<conio.h>
#define MAX_SIZE 100  // Maximum string size, change to make string smaller or larger
#define MAX_CHARS 255 // Maximum characters allowed for characters


void main()
{
    char str[MAX_SIZE];  //store the string
    int freq[MAX_CHARS]; // store frequency of each character
    int i, max; // i is for loop max to store frequency
    int ascii;   //stores ascii value convertd from each char
    char ch;    //for choice

    do{
        clrscr();
        i=0;
    printf("\nEnter any string: ");
    gets(str);

    // Initializes frequency of all characters to 0
    for(i=0; i<MAX_CHARS; i++)
    {
    freq[i] = 0;
    }


    // Finds occurance/frequency of each characters
    i=0;
    while(str[i] != '\0')
    {
    ascii = (int)str[i];
    freq[ascii] += 1;       //string's element is casted to int to store its ascii value for further comparision

    i++;
    }


    // Finds maximum frequency of character
    max = 0;
    for(i=0; i<MAX_CHARS; i++)
    {
    if(freq[i] > freq[max])
        max = i;            //to print no. of times 
    }


    printf("\nMaximum occurring character is '%c' = %d times.", max, freq[max]);
    printf("\n Want to find again??(y/n):");
    scanf("%c",&ch);
    }while(ch=='Y'||ch=='y');
}

When I give it the input: "aaaaeeee", the output is "a" occurring 4 times, but "e" occurs 4 times too. I know this is sorted by ascii values and thats why it gives "a" as output, but what can I do in this program that the output gives both "a" and "e" as output when a case like this occurs?

5
  • 2
    side note: Don't use void main(), use int main()..And conio.h header as well. Commented Nov 21, 2017 at 9:02
  • I am new to coding stuff, whats the difference? Commented Nov 21, 2017 at 9:03
  • Please go through this question, where the return type of main is discussed in detail: stackoverflow.com/questions/204476/… Commented Nov 21, 2017 at 9:05
  • 1
    And never, never, never use gets, use fgets (or POSIX getline) gets is so insecure and so vulnerable to buffer overrun, it has been removed from the C11 library. Toss it. If your prof wants you to use it, toss him too. Commented Nov 21, 2017 at 9:05
  • this isnt college work or anything, its for my own curiosity to learn coding, and i will take care of it. Commented Nov 21, 2017 at 9:16

2 Answers 2

2

Add max calculation ahead

 i = 0;
 max = 0;
 while(str[i] != '\0')
 {
    ascii = (int)str[i];
    freq[ascii] += 1;
    if (freq[ascii] > max) max = freq[ascii]; // <==== here
    i++;
 }

Note that this is the max number of the same character you might have.

Then display all chars which maximum is equal to max

for(i=0; i<MAX_CHARS; i++)
{ 
   if(freq[i] == max) printf("Character %c is at max %d\n", i, max);
}

To fix the endless loop, before the while add char c ; while ((c = getchar()) != EOF && c != '\n');

   scanf("%c",&ch);
   char c;
   while ((c = getchar()) != EOF && c != '\n'); // <== note the ';'
} while(ch=='Y'||ch=='y');

Note that you shouldn't use gets, reason is explained here.


Whole code:

void main()
{
    char str[MAX_SIZE];  //store the string
    int freq[MAX_CHARS]; // store frequency of each character
    int i, max; // i is for loop max to store frequency
    int ascii;   //stores ascii value convertd from each char
    char ch;    //for choice

    do {
        printf("\nEnter any string: ");
        gets(str);

        // Initializes frequency of all characters to 0
        for(i=0; i<MAX_CHARS; i++)
        {
            freq[i] = 0;
        }


        // Finds occurance/frequency of each characters
        for(i=0,max=0 ; str[i] != '\0' ; i++)
        {
            ascii = (int)str[i];
            freq[ascii] += 1;       //string's element is casted to int to store its ascii value for further comparision
            if (freq[ascii] > max) max = freq[ascii];
        }

        for(i=0; i<MAX_CHARS; i++)
        { 
            if(freq[i] == max) printf("Character %c is at max %d\n", i, max);
        }

        printf("\n Want to find again??(y/n):");
        scanf("%c",&ch);
        char c;
        while ((c = getchar()) != EOF && c != '\n'); 
    }while(ch=='Y'||ch=='y');
}
Sign up to request clarification or add additional context in comments.

4 Comments

theres just one problem, when i re-run the code by typing 'y' when prompted if i wanna find again or not, at that time the output shows every character in ASCII, and does not take input
See stackoverflow.com/questions/24073138/… . Basically add char c ; while ((c = getchar()) != EOF && c != '\n'); before the ending while.
the output is wrong after code is re-run, even without re-run, its wrong in somecases like "aaaa" output is "Character a is at max 2"
Of course the i++ had to be removed in the 'whole code' - I changed the i=0 ; while ... to for ... thus i was incremented twice. Code edited!
1

Above this line

printf("\nMaximum occurring character is '%c' = %d times.", max, freq[max]);

Delete it and add this code

for(i=0;i<MAX_CHARS;i++)
{
    if(freq[i]==freq[max])
    {
        printf("\nMaximum occurring character is '%c' = %d times.", i, freq[i]);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.