C program to find highest frequency character in a string

Category: C Program
Tags: #cprogram#string

Learn how to write a C program to find the highest frequency character in a string. This comprehensive guide includes step-by-step instructions, code implementation, and detailed explanations for beginners in C programming.

In this article, we will explore a C program designed to find the character that occurs most frequently in a given string. This is a common problem in text processing and can be useful in various applications such as text analysis, data compression, and natural language processing.

Objective

The aim is to create a C program that accepts a string as input and determines which character appears most frequently. If multiple characters have the same highest frequency, the program will return the first one encountered.

Approach

To solve this problem, we will:

  1. Read the input string from the user.
  2. Use an array to count the frequency of each character.
  3. Identify the character with the highest frequency.
  4. Output the highest frequency character along with its count.

Detailed Steps

  1. Read the input string:
    • Use standard input functions to get the string from the user.
  2. Count character frequencies:
    • Initialize an array to keep track of the frequency of each character.
    • Iterate over the string and update the frequency array.
  3. Find the highest frequency character:
    • Iterate over the frequency array to find the character with the highest frequency.
  4. Print the result:
    • Output the character with the highest frequency and its count.

Write a C program to find highest frequency character in a string

Below is the complete C program implementing the above steps:

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

#define ASCII_SIZE 256

int main() {
    char str[100];
    int freq[ASCII_SIZE] = {0};
    int max_freq = 0;
    char max_char;

    // Input the string
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    // Remove newline character from string if present
    size_t len = strlen(str);
    if (len > 0 && str[len - 1] == '\n') {
        str[len - 1] = '\0';
    }

    // Count frequency of each character
    for (int i = 0; i < strlen(str); i++) {
        freq[(int)str[i]]++;
    }

    // Find the character with the highest frequency
    for (int i = 0; i < ASCII_SIZE; i++) {
        if (freq[i] > max_freq) {
            max_freq = freq[i];
            max_char = (char)i;
        }
    }

    // Print the highest frequency character and its count
    printf("The highest frequency character is '%c' with a frequency of %d.", max_char, max_freq);

    return 0;
}

Output

Enter a string: learn programming at ProCoding
The highest frequency character is 'r' with a frequency of 4.

Explanation of the Program

  1. Input Handling:
    • The program uses fgets() to read a line of text from the user, ensuring that spaces and special characters are included.
    • It removes any trailing newline character added by fgets().
  2. Frequency Count:
    • An array freq of size 256 (to cover all ASCII characters) is initialized to zero.
    • int freq[ASCII_SIZE] = {0}; this is a shorthand way to initialize every element in the array to 0.
    • The program iterates over each character in the string, incrementing the corresponding index in the freq array based on the ASCII value of the character.
  3. Identify Highest Frequency Character:
    • The program iterates over the freq array to find the maximum frequency and the corresponding character.
  4. Output:
    • The character with the highest frequency and its count are printed.

Recommended Posts