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, StringLogic 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.- 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. - Before you begin any processing with the string make sure that the array elements are initialized to 0.
- Then for each character ch in the string repeat the next step.
- 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)
-
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
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 ;)
You may also like
- String programming exercises index.
- C program to count frequency of each element in an array.
- C program to find length of a string.
- C program to convert uppercase string to lowercase string.
- C program to convert lowercase string to uppercase string
- C program to find reverse of a given string.
- C program to check whether a string is palindrome or not.
- C program to count number of vowels and consonants in a string.
- C program to count total number of words in a string.
- C program to find first occurrence of a character in the string.
- C program to remove first occurrence of a character from the string.