Algorithm and flowchart to find Frequency of Characters in Sentence

[2818 views]


In this program we will be counting the Frequency of characters.
Input: charger
Output: c-1, h-1, a-1, r-2, g-1, e-1.
You should have some knowledge of ASCII values or you can just check the ASCII table while understanding below code.

What is the Approach for this program ?

It is clear that for counting every character. We need to take a array of length of 26 to store the frequency of each letter. To enter the frequency we have to assume a as the starting point and as 0. So freq[0] means frequency of a. Every character and digit has been given a ASCII values. Like a is 97 ,b is 98 and it goes on. So we will be using these ASCII values to map it to the array and add the corresponding block of array by 1.

Flowchart for Counting frequency of characters in string

Flowchart for Counting frequency of characters in string
Remove WaterMark from Above Flowchart

Algorithm for Counting frequency of Characters in string:

Declare array of character ,array of int and a variable i; //for storing the string , frequency and running the loop Read the string ; for each character in string s { frequency[string[i]-'a']++; } for each block in a frequency { check if(block is not 0) { print frequency; } }

Here in this algorithm we declare an array of character string[40] to store the string and array of integers frequency[26] to store the frequency of characters of each alphabet and a variable i to run the loop. In this we will be using for loop to run each character of the string. For making a = 0 ,b = 0 etc. We have used ASCII values for that like we will be subtracting 'a' from each character of the string. For eg. we took a string 'dark', so when the d(ASCII value=100) enters the loop it will be subtracted from a(ASCII value=97) we get 3 which will act as index for the frequency array. Like 3 will be the 4th place of frequency array and then at that place we will increment a value. Next comes character a value will be 0 which will be the 0th place of frequency array and increment the value. With the rest values we follow the same procedure.

Now for printing the frequency we run the loop till 26 as there 26 alphabets and check if there is a block with not 0, print that block.

C Program to count number of Characters in String

#include <stdio.h> #include <string.h> int main() { char string[40]; //input string int i, frequency[26] = { 0 }; //initialize the array scanf("%s", string); for (i = 0; i < strlen(string); i++) { frequency[string[i] - 'a']++; //Taking a as starting point //ASCII values are there starting from 97 } for (i = 0; i < 26; i++) { if (frequency[i] != 0) check block is not empty { printf("%c - %d\n", i + 'a', frequency[i]); // i + 'a' will add the number and give its character // and frequency[i] will give the count } } }

Output of the program:

        

Don't Understand Algorithm and Flowchart? Learn quickly using our Expert's Notes



Want to get in-depth understanding of Algorithms?


Are You Good enough in Algorithms? Test it now?


Comments



Search
Recommended Deals End in










Java Interview Questions For Beginner Notes
Quiz for Learners
Online Games
Play 2048 Game Online and Relax.
Play 2048 Game Online

Search Tags

    Algorithm to find Frequency of Characters in String

    Count number of characters in a string algorithm

    Calculate number of characters in string C program