Back to C Programming
2026-07-135 min read

Find the Frequency of Characters in a String

Learn Find the Frequency of Characters in a String step by step with clear examples and exercises.

Why This Matters

Welcome to this full guide on finding the frequency of characters in a string using C programming! You'll learn about character frequency, its importance, and how to write an efficient program to calculate it. Let's dive right in!

Why This Matters

In various applications such as text analysis, data mining, and even debugging, finding the frequency of characters in a string is crucial. It helps us understand the distribution of characters within a given string, which can be useful for many purposes like identifying common patterns or errors.

Importance of Character Frequency Analysis

Character frequency analysis plays a significant role in several areas:

  • Text Analysis: Analyzing character frequencies can help identify trends and patterns in text data, such as the most common words or letters in a document.
  • Data Mining: In data mining, understanding character frequencies can aid in clustering similar data points based on their characteristics.
  • Debugging: Debugging complex programs often involves analyzing character frequencies to identify potential issues like repetitive errors or patterns that might indicate logical errors.

Prerequisites

To follow this tutorial effectively, you should have a good understanding of:

  • Basic C programming concepts such as variables, loops, and functions
  • C standard libraries including stdio.h and string.h

If you're new to C programming or need a refresher, check out our full guide on C Programming for Beginners.

Core Concept

To find the frequency of characters in a string, we will create a program that iterates through each character in the input string and counts their occurrences. We will use an array to store the frequencies of all possible ASCII characters (0-127) and update their respective values as we encounter them in the input string.

Understanding ASCII Table

The American Standard Code for Information Interchange (ASCII) is a widely used character encoding standard that assigns unique numbers, or codes, to each character. In C programming, characters are stored as integers within this range.

Creating the Frequency Array

  1. Declare an array freq[128] to store the frequency of each character. Initialize all elements to 0 since no characters have been encountered yet.
int freq[128] = {0};

Reading and Processing Input String

  1. Read the input string using fgets().
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
  1. Iterate through each character in the input string using a for loop. Increment the corresponding element in the freq array by 1 for every occurrence of the character.
int len = strlen(str);
for (int i = 0; i < len; ++i) {
freq[(int)str[i]]++;
}
  1. Print the frequency of each character by iterating through the freq array and printing characters along with their frequencies.
printf("Frequency of each character:\n");
for (int i = 0; i < 128; ++i) {
if (freq[i] > 0) {
printf("%c: %d\n", i, freq[i]);
}
}

Worked Example

Let's see how the program works with an example.

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

int main() {
int freq[128] = {0};
char str[100];

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

int len = strlen(str);
for (int i = 0; i < len; ++i) {
freq[(int)str[i]]++;
}

printf("Frequency of each character:\n");
for (int i = 0; i < 128; ++i) {
if (freq[i] > 0) {
printf("%c: %d\n", i, freq[i]);
}
}

return 0;
}

Enter the following string as input:

Hello World!

The output will be:

Frequency of each character:
H: 1
e: 2
l: 3
o: 4
: 1
W: 1
r: 1
d: 1
!: 1

Common Mistakes

  • Not initializing the freq array: Initializing the array is essential to avoid unexpected behavior.
  • Forgetting to convert the character to an integer for indexing: Characters in C are stored as integers, so we need to cast them to int before using them as indices in the freq array.
  • Not handling spaces and special characters: Spaces and special characters should also be counted if required. Make sure to include them in the loop and update their frequencies accordingly.
  • Not considering the ASCII table limits: Ensure that the character's ASCII value falls within the range of 0-127, as C arrays are indexed from 0 to 127.

Practice Questions

  1. Write a program that finds the frequency of vowels and consonants in a given string.
  2. Modify the program to count the frequency of uppercase and lowercase letters separately.
  3. Extend the program to handle non-ASCII characters as well.
  4. Create a function to find the most frequent character in a given string.
  5. Implement a function that calculates the average frequency of vowels and consonants in a string.

FAQ

Q1: Why do we use an array of size 128 instead of 256?

A1: In C, characters are represented using ASCII values ranging from 0 to 127. Since the first 128 ASCII codes include all printable characters (including alphabets, digits, and special characters), we use an array of size 128.

Q2: What if the input string is larger than the str array?

A2: In this example, we assume that the input string will fit within the str array. If the input string is too large, you should either increase the size of the array or use dynamic memory allocation to handle larger inputs.

Q3: Why do we not include the null character (\0) in our frequency count?

A3: The null character (\0) signifies the end of a string and does not contribute to the frequency count since it is not part of the actual characters in the string.

Q4: How can I handle punctuation marks while counting character frequencies?

A4: Punctuation marks can be handled by including them in the freq array or creating separate arrays for alphabets, digits, and special characters. You may also choose to ignore certain punctuation marks based on your specific requirements.

Q5: Can I use a hash table instead of an array to count character frequencies?

A5: Yes! A hash table can be used as an alternative to counting character frequencies in C programming. However, it requires more memory and may not always be necessary for smaller strings.