In this program we will be counting the Frequency of characters.
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.
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 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.
Here in this algorithm we declare an array of character string to store the string and array of integers frequency 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.
Our Quiz prepared by Experts Helps you identify your knowledge in Algorithms. Everyone should atleast attempt this Quiz Once.