This is a C program to find the most/least repeated character in the string.
This C Program finds the most/least repeated character in the string.
Take input from the user and displays most/least repeated characters as shown in the program below.
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; }
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.
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.
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.
$ 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.
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
- Watch Advanced C Programming Videos
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Check Computer Science Books
- Apply for C Internship