C program to count frequency of each character in a string

Write a C program to count frequency of each character in a string using loop. How to find frequency of each characters in a string in C programming.

Example:
Input string: Hello
Output frequency of each character:
e=1
h=1
l=2
o=1

Required knowledge

Basic C programming, If else, For loop, Array, String

Logic to count frequency of each character

There are many algorithms to count frequency of each character. Here I am explaining the simplest one which goes below.
  1. To count and store frequency of each alphabet we need an array say it as freq[26]. An array of size 26 (since there are 26 alphabets). Each element of the array will hold the occurrence of specific alphabet. For example
    array[0] will hold the occurrence of a alphabet, similarly
    array[1] will hold frequency of b and so on
    array[25] will hold frequency of z.
  2. Before you begin any processing with the string make sure that the array elements are initialized to 0.
  3. Then for each character ch in the string repeat the next step.
  4. If ch == 'a' then increment the value of freq[0]++ by one. Similarly if ch == 'z' then increment freq[25]++.

    Now, to make things little easier and to work with every character in the given string we use below logic to increment freq.
    We use freq[ch - 97] (For lowercase alphabets)
    And freq[ch - 65] (For uppercase alphabets)
  5. Now, why we have used these values ch - 97 and what it will do? We know that the ASCII value of a is 97, b is 98 and so on. Suppose ch = 'c' then we need to increment the value of freq[2]++ by one.
    Lets, do a simple mathematics
    => freq[ ch - 97 ]
    => freq[ 99 - 97 ] (since ASCII value of c = 97)
    => freq[ 2 ]
    Which works.
    You can apply the same mathematics with the upper case alphabets i.e. if ch = 'Z' then we need to perform freq[ ch - 65 ].


Program to count frequency of each character

/**
 * C program to count frequency of each character in a string
 */

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

#define MAX_SIZE 100 //Maximum size of the string

int main()
{
    char string[MAX_SIZE];
    int i, len;
    int frequency[26];

    /* Reads a string from user */
    printf("Enter any string: ");
    gets(string);

    len = strlen(string);

    /* Initializes frequency of each character to 0 */
    for(i=0; i<26; i++)
    {
        frequency[i] = 0;
    }


    /* Finds total number of occurrences of each character */
    for(i=0; i<len; i++)
    {

        /* If the current character is lowercase alphabet */
        if(string[i]>='a' && string[i]<='z')
        {
            frequency[string[i] - 97]++;
        }
        else if(string[i]>='A' && string[i]<='Z')
        {
            frequency[string[i] - 65]++;

        }
    }

    /*
     * Prints the frequency of all characters in the string
     */
    printf("\nFrequency of all characters in the given string: \n");
    for(i=0; i<26; i++)
    {
        /* If the current character is in given string */
        if(frequency[i]!=0)
        {
            printf("%c = %d\n", (i+97), frequency[i]);
        }
    }

    return 0;
} 


Output
Enter any string: I love C programming!

Frequency of all characters in the given string:
a = 1
c = 1
e = 1
g = 2
i = 2
l = 1
m = 2
n = 1
o = 2
p = 1
r = 2
v = 1

Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment