Finding characters frequency of a String means calculating the occurrence of each alphabet present in the string. Since there's no pre-build function present in Java for calculating the occurrence of the characters or alphabets so, we have to write a program which will give the frequency of characters present in the string. Let's get started.
Than we have to traverse the string 26 times in search of all the 26 alphabets one by one.
If the string character matches to the searching alphabet than we have to increment the value stored at that particular index of int array by 1.
confused? have a look at the source code given below.
Here we made a function named charFreq which takes the string as an argument and returns an array of integers which have the calculated frequency of all the alphabets present in the input string.
Firstly we converted the entire string to lower-case characters for the further operations and defined an integer array freq[] of size 26 (default value of all the elements in an integer array is zero).
Then as mentioned in the Logic above we have to traverse the string 26 times for each and every character on by one. Here we are taking alphabets as 97, 98, 99 and so on which is the ascii value of lower-case alphabets. when the string character matches with our searching alphabet then we increase the value at that index in an array freq[i]++. Finally at the end the function returns an array of integers having the frequency of characters.
Using the Function: int array[] = charFreq( "you string here" );
Extracting value from array: array[ index_between_1_to_26 ]
e.g array[2] will give the occurrence of 'c'
If you like this post please like and share with your friends and feel free to coment if you have any queries related to this program.
Logic: Characters Frequency in a String
First we have take an integer array of size 26 having all its elements equal to zero.Than we have to traverse the string 26 times in search of all the 26 alphabets one by one.
If the string character matches to the searching alphabet than we have to increment the value stored at that particular index of int array by 1.
confused? have a look at the source code given below.
Source Code
class CharFrequency { static int[] charFreq(String s){ s=s.toLowerCase(); //convert to lowercase alphabets int freq[]=new int[26]; //array for counting freq of all alphabets for(int i=0,c=97; i<26; i++,c++){ for(int j=0; j<s.length(); j++){ char ch=s.charAt(j); //traversing the string if(ch==c) //checking string for 'a' i.e 97 then 'b' i.e 98 and so on freq[i]++; //increasing count of those aplhabets which are present in the string } } return freq; //returning array having freq of alphabests present in String s } public static void main(String args[]){ String s = "Java Programming"; //Getting the frequency int freq[]=charFreq(s); //printing the output System.out.println("Alphabet\tFrequency"); for(int i=0,c=97; i<26; i++,c++){ if(freq[i]!=0){ //alphabet having freq > 0 char ch = (char) c; System.out.println(ch+"\t\t"+freq[i]); } } } }
Output
Explanation
This is the most basic approach for finding the characters frequency.Here we made a function named charFreq which takes the string as an argument and returns an array of integers which have the calculated frequency of all the alphabets present in the input string.
Firstly we converted the entire string to lower-case characters for the further operations and defined an integer array freq[] of size 26 (default value of all the elements in an integer array is zero).
Then as mentioned in the Logic above we have to traverse the string 26 times for each and every character on by one. Here we are taking alphabets as 97, 98, 99 and so on which is the ascii value of lower-case alphabets. when the string character matches with our searching alphabet then we increase the value at that index in an array freq[i]++. Finally at the end the function returns an array of integers having the frequency of characters.
Using the Function: int array[] = charFreq( "you string here" );
Extracting value from array: array[ index_between_1_to_26 ]
e.g array[2] will give the occurrence of 'c'
Conclusion
There are many other logics by which we can perform the same task, above logic is the most simple approach for finding the character frequency of a string. You may try your own logic and do share with us!If you like this post please like and share with your friends and feel free to coment if you have any queries related to this program.
Leave a Reply
Make sure you tick the "Notify Me" box below the comment form to be notified of follow up comments and replies.