Count Frequency of Characters in String in C Language

Count-Frequency-of-Characters-in-String-in-C-Language-program

Program Description:

Write a Program to Count Frequency of Characters in String in C Programming language. The program should accept a string from the user and count the frequency of each character in the given string. This program is used to know how many times each character is present in the string.

We are going to look at the few methods to count the character frequencies and each program will be followed by a detailed step-by-step explanation of the program and program output. We also included the instructions to compile and run the program.

📢 This Program is part of the C String Practice Programs series

Example Input and Output:

Input:

Enter a string : www.SillyCodes.com

Output:

The program will also count the frequency of the special characters.

Prerequisites:

It is recommended to know the C-Strings, C-Arrays, and Functions in C to better understand the following program.

We will look at the two different methods to count the frequency of characters in a string in c.

  • Iterative Method
  • Using User-defined Functions

Algorithm to Count Frequency of Characters in String in C:

The main idea to count the frequency of characters in a string is to maintain a frequencyarray/frequency string. The frequency string will have 256 characters. Why 256 characters? because it is the number of ASCII values. i.e – frequency[256]

Iterate through the all characters of the Input string and update the frequencyarray.

frequency[inputStr[i]]++;

For example, If the input string has a character a, Then we will update the frequency array at the index 97.

frequency[‘a’]

The ASCII equivalent of the ‘a’ is 97. So the count of the character ‘a’ will be stored at the 97th index of the frequencyarray.

frequency[97]

Initially, Frequency array elements are initialized with zeroes. The character index (i.e 97th here) will be incremented based on the input string characters.

frequency[97]++;

This way each character frequency will be updated in the frequency array. Finally, Display the frequency of each character on the console.

Let’s look at the step-by-step explanation of the program.

Count Frequency of Characters in a String in C Program Explanation:

  1. Start the Program by defining Two Macros named SIZE and ASCIISIZE with 100 and 256 values respectively.
  2. Declare two string to store the input string( inputStr) and Frequency of characters( frequency) string. – i.e char inputStr[SIZE]; and char frequency[ASCIISIZE] = {0};
  3. Read the input string from the user using the gets function and store it in inputStr string.
  4. To count the frequency of characters in a string, Iterate over the string and update the frequency array based on the characters.
  5. Create a For loop to Iterate over the string. Start the loop from i=0 and go till the terminating NULL character. i.e for(i = 0; inputStr[i]; i++)
    • At each iteration, update the frequency array based on the ASCII value of the inputStr[i] – i.e frequency[inputStr[i]]++;
  6. Once the above loop is completed, The frequency array will contain the frequency of each character in the given string.
  7. Iterate over the frequency array and print the frequencies using another loop
  8. Stop the program.

Program to Count Frequency of Characters in String in C Iterative Method:

Here is the program to check the frequency of each character in a string

Program Output:

Let’s compile and Run the Program using your compiler. We are using GCC compiler in Ubuntu Linux OS.

Example 2:

Count-Frequency-of-Characters-in-a-String-Program-output

As we can see from the above output, The program is properly counting the frequencies of the characters in the given string.

Count Frequency of Characters in String in C using a user-defined function:

In this method, We are going to move the frequency counting logic to a user-defined function, So that we can reuse the code and also functions improve the readability of the program.

Let’s rewrite the above C program and use a function to print the frequencies of the character in a string.

In the above program, We have defined a function named printFrequencies. The printFrequencies function takes a string as formal argument and counts the character frequencies of the input string.

Here is the prototype of the printFrequencies() function.

void printFrequencies(char *str)

In the main() function, Get the input string from the user and store it in inputStr string. Then call the printFrequencies() function with the inputStr to print the frequencies of each character in the inputStrstring.

printFrequencies(inputStr);

This printFrequencies function doesn’t return anything. So return type of the function is void

Program Output:

Compile and Run the program.

Count-Frequency-of-Characters-in-a-string-using-functions-program-output

We have provided the input string as C Programming Language and the program calculated the frequencies of each character and printed on the console.

Related String Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

2 Responses

  1. […] C Program to Count Frequencies of each character in a string […]

  2. […] C Program to Count Frequencies of each character in a string […]

Leave a Reply