C Program to Find the Most and Least Repeated Character in the String

This is a C program to find the most/least repeated character in the string.

Problem Description

This C Program finds the most/least repeated character in the string.

Problem Solution

Take input from the user and displays most/least repeated characters as shown in the program below.

Program/Source Code

Here is a source code of the C program to find the most/least repeated character in the string. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 *C Program to Find the Most/Least Repeated Character in the String
 */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
 
struct detail
{
    char c;
    int freq;
};
 
int main()
{
    struct detail s[26];
    char string[100], c;
    int max[26] = {0}, min[26] = {0};
    int i = 0, index, maxcount = 1, mincount = 1000, j;
 
    for (i = 0; i < 26; i++)
    {
       s[i].c = i + 'a';
       s[i].freq = 0;
    }
    printf("Enter string: ");
    i = 0;
    do
    {
        fflush(stdin);
        c = getchar();
        string[i++] = c;
        if (c == '\n')
        {
            break;
        }
        else if (!isalpha(c))
        {
            continue;
        }
        c = tolower(c);
        index = c - 'a';
        s[index].freq++;
    } while (1);
    string[i - 1] = '\0';
    printf("The string entered is: %s\n", string);
    for (i = 0; i < 26; i++)
    {
        if (s[i].freq)
        {
            if (maxcount < s[i].freq)
            {
                for (j = 0; j < 26; j++)
                {
                    max[j] = 0;
                }
                max[i] = 1;
                maxcount = s[i].freq;
            }
            else if (maxcount == s[i].freq)
            {
                max[i] = 1;
            }
            if (mincount >= s[i].freq)
            {
                if (mincount == s[i].freq)
                {
                    min[i] = 1;
                }
                else
                {
                    for (j = 0; j < 26; j++)
                    {
                        min[j] = 0;
                    }
                    min[i] = 1;
                    mincount = s[i].freq;
                }
            }
        }
    }
    printf("The most repeated characters are: ");
    for (i = 0; i < 26; i++)
    {
        if (max[i])
        {
            printf("%c ", i + 'a');
        }
    }
    printf("\nThe least repeated characters are: ");
    for (i = 0; i < 26; i++)
    {
        if (min[i])
        {
            printf("%c ", i + 'a');
        }
    }
    printf("\n");
 
    return 0;
}
Program Explanation

In this C Program, do while loop is used for reading a string using ‘c’ variable. If else condition statement is used to check that the entered character is empty i.e, is space or enter. If the condition is true then execute the statement.

advertisement
advertisement

Otherwise, if the condition is false, then execute the else if condition statement. Check the entered character is numeric character or not. If the condition is true, then execute the statement. Convert the value of string variable into lower case.

For loop is used to find the most or least repeated character in the string. Nested if else condition statement is used to check the value of ‘maxcount’ variable is less than the number of characters entered. If the condition is true, then execute the statement.

Otherwise if the condition is false, then execute the else if condition statement. Check the value of ‘maxcount’ variable is equal to the number of characters entered. If the condition is true, then execute the statement and initialize the value of ‘max[]’ variable as 1.

Note: Join free Sanfoundry classes at Telegram or Youtube

Another nested if condition statement is used to check the value of ‘mincount’ variable is greater than or equal to the number of characters entered. If the condition is true, then execute the statement. Again check the condition that the value of ‘mincount’ variable is equal to the number of characters occurred in the string.

If the condition is true then execute the statement and assign the value of 1 to the ‘min[]’ array variable. Otherwise, if the condition is false then execute the else condition statement. Using for loop prints the most and least characters in the string.

Runtime Test Cases
 
$ gcc minmaxchar.c 
$ ./a.out
Enter string: I love C programming
The string entered is: I love C programming
The most repeated characters are: g i m o r 
The least repeated characters are: a c e l n p v

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.