C Program to Count the Frequency of Each Character in a String

Introduction

Counting the frequency of each character in a string involves determining how many times each character appears. This is useful in various text processing tasks, such as analyzing the content of a string. This guide will show you how to write a C program to count the frequency of each character in a string.

Problem Statement

Create a C program that:

  • Takes a string as input from the user.
  • Counts the frequency of each character in the string.
  • Displays the frequency of each character.

Example:

  • Input: String = "Hello, World!"
  • Output:
    • H: 1
    • e: 1
    • l: 3
    • o: 2
    • ,: 1
    • : 1
    • W: 1
    • r: 1
    • d: 1
    • !: 1

Solution Steps

  1. Include the Standard Input-Output Library: Use #include <stdio.h> for standard input-output functions.
  2. Write the Main Function: Define the main function, which is the entry point of every C program.
  3. Declare Variables: Declare variables to store the input string, a frequency array, and a loop counter.
  4. Input the String: Use gets or scanf to take input from the user for the string.
  5. Initialize the Frequency Array: Initialize an array to store the frequency of each character. The array size should be large enough to handle all possible ASCII characters.
  6. Count the Frequency of Each Character: Use a loop to iterate through the string and update the frequency array for each character.
  7. Display the Frequency of Each Character: Use another loop to display the characters and their corresponding frequencies.

C Program to Count Frequency of Each Character in a String

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

int main() {
    // Step 1: Declare variables to hold the input string and frequency array
    char str[100];
    int frequency[256] = {0};  // Array to store frequency of characters, initialized to 0
    int i;

    // Step 2: Prompt the user to enter a string
    printf("Enter a string: ");
    gets(str);  // Using gets to read the string including spaces

    // Step 3: Count the frequency of each character
    for (i = 0; str[i] != '\0'; i++) {
        frequency[(int)str[i]]++;  // Increment the frequency of the character
    }

    // Step 4: Display the frequency of each character
    printf("Character frequencies:\n");
    for (i = 0; i < 256; i++) {
        if (frequency[i] != 0) {
            printf("'%c': %d\n", i, frequency[i]);
        }
    }

    return 0;  // Step 5: Return 0 to indicate successful execution
}

Explanation

Step 1: Declare Variables

  • The variable str is declared to store the input string. The array frequency[256] is used to store the frequency of each character, with the size 256 to accommodate all possible ASCII characters. The variable i is used as a loop counter.

Step 2: Input the String

  • The program prompts the user to enter a string using printf. The gets function is used to read the string, allowing it to include spaces.

Step 3: Count the Frequency of Each Character

  • The program uses a for loop to iterate through each character of the string:
    • The ASCII value of the character is used as an index to increment the corresponding element in the frequency array.

Step 4: Display the Frequency of Each Character

  • The program uses another for loop to iterate through the frequency array:
    • For each non-zero value in the array, the corresponding character and its frequency are displayed.

Step 5: Return 0

  • The return 0; statement indicates that the program executed successfully.

Output Example

Example 1:

Enter a string: Hello, World!
Character frequencies:
'H': 1
'e': 1
'l': 3
'o': 2
',': 1
' ': 1
'W': 1
'r': 1
'd': 1
'!': 1

Example 2:

Enter a string: C Programming
Character frequencies:
'C': 1
' ': 1
'P': 1
'r': 2
'o': 1
'g': 2
'a': 1
'm': 2
'i': 1
'n': 1

Conclusion

This C program demonstrates how to count the frequency of each character in a string by iterating through the string and using an array to store the frequencies. It covers basic concepts such as string manipulation, loops, arrays, and ASCII values, making it a useful example for beginners learning C programming.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top