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: 1e: 1l: 3o: 2,: 1: 1W: 1r: 1d: 1!: 1
Solution Steps
- Include the Standard Input-Output Library: Use
#include <stdio.h>for standard input-output functions. - Write the Main Function: Define the
mainfunction, which is the entry point of every C program. - Declare Variables: Declare variables to store the input string, a frequency array, and a loop counter.
- Input the String: Use
getsorscanfto take input from the user for the string. - 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.
- Count the Frequency of Each Character: Use a loop to iterate through the string and update the frequency array for each character.
- 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
stris declared to store the input string. The arrayfrequency[256]is used to store the frequency of each character, with the size 256 to accommodate all possible ASCII characters. The variableiis used as a loop counter.
Step 2: Input the String
- The program prompts the user to enter a string using
printf. Thegetsfunction is used to read the string, allowing it to include spaces.
Step 3: Count the Frequency of Each Character
- The program uses a
forloop 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
frequencyarray.
- The ASCII value of the character is used as an index to increment the corresponding element in the
Step 4: Display the Frequency of Each Character
- The program uses another
forloop to iterate through thefrequencyarray:- 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.