Find the Most Frequently Occurring Character in a String in C

To find the most frequently occurring character in a string in C, we can use techniques such as an array to store character counts, a hash table, or sorting-based approaches. The basic idea is to iterate through the string, count occurrences of each character, and determine which character appears the most.

In this tutorial, we will explore multiple methods to achieve this with clear explanations and examples.


Examples to Find the Most Frequent Character

1. Using an Array to Store Character Frequency

In this example, we will count the occurrences of each character using an array of size 256 (to accommodate all ASCII characters). We will then find the character with the highest frequency.

main.c

</>
Copy
#include <stdio.h>
#include <string.h>

#define ASCII_SIZE 256

char findMostFrequentChar(char str[]) {
    int count[ASCII_SIZE] = {0}; 
    int max = 0;
    char result;

    for (int i = 0; str[i] != '\0'; i++) {
        count[(int)str[i]]++;
        if (count[(int)str[i]] > max) {
            max = count[(int)str[i]];
            result = str[i];
        }
    }

    return result;
}

int main() {
    char str[] = "hello world";
    char mostFrequent = findMostFrequentChar(str);
    printf("Most frequent character: %c\n", mostFrequent);
    return 0;
}

Explanation:

  1. We define a function findMostFrequentChar() that takes a string as input.
  2. An integer array count[256] is used to store the frequency of each character.
  3. We iterate through the string and update the frequency count using count[(int)str[i]]++.
  4. Each time we update the count, we check if it is greater than the current maximum and update result.
  5. Finally, the function returns the most frequently occurring character.

Output:

Most frequent character: l

2. Using Sorting to Find the Most Frequent Character

In this example, we first sort the string, then count the frequency of consecutive characters to determine the most frequent one.

main.c

</>
Copy
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int compare(const void *a, const void *b) {
    return *(char *)a - *(char *)b;
}

char findMostFrequentSorted(char str[]) {
    int len = strlen(str);
    qsort(str, len, sizeof(char), compare);

    char maxChar = str[0];
    int maxCount = 1, count = 1;

    for (int i = 1; i < len; i++) {
        if (str[i] == str[i - 1]) {
            count++;
        } else {
            if (count > maxCount) {
                maxCount = count;
                maxChar = str[i - 1];
            }
            count = 1;
        }
    }

    return (count > maxCount) ? str[len - 1] : maxChar;
}

int main() {
    char str[] = "apple banana";
    char mostFrequent = findMostFrequentSorted(str);
    printf("Most frequent character: %c\n", mostFrequent);
    return 0;
}

Explanation:

  1. We define a compare() function for qsort() to sort the string.
  2. We sort the string using qsort() so that repeated characters appear consecutively.
  3. We traverse the sorted string to count consecutive occurrences of each character.
  4. If a character’s count exceeds maxCount, we update maxChar.
  5. Finally, we return the most frequently occurring character.

Output:

Most frequent character: a

Conclusion

In this tutorial, we explored different methods to find the most frequently occurring character in a string:

  1. Using an array: Efficient method that tracks character frequencies using an array of size 256.
  2. Using sorting: Sorts the string first and then counts consecutive occurrences.

Both methods have their advantages. The array method is more efficient, while sorting provides an alternative approach.