Hello people! today we will learn Java Program to find the frequency of characters. I’m going to explain the procedure and the code.
Java Program to find the frequency of characters
Firstly let’s understand the question, we need to find the count of the characters of a phrase or a word. So let’s work out the steps to get the frequency.
- Convert the string to lowercase: we will convert the whole phrase or word to lowercase as lowercase and uppercase letters have different ASCII values and would be difficult to get the count. Thus ensuring that ‘A’ and ‘a’ are counted as the same character.
- Initialize an integer array of size 26: we are taking this array to store the frequency of each letter from ‘a’ to ‘z’.
- Iterate through the string: Then, we will take a for-loop and store the frequencies of the alphabet here for this, we are going to use ASCII values, it might be difficult to understand this so let’s break it down I have the character ‘c’ and according to the array I need to store the frequency of the character ‘c’ in the 2nd index(array starts from 0 index) as ‘b’ is the second letter so to get 2 we will be subtracting the char ‘c’ with ‘a’. when we write down [‘c’-‘a’] we subtract the ASCII values of the characters instead of the characters themselves Thus getting the answer as 2(99-97).
- Print the frequencies: Now that we have the characters’ frequencies, we will print each of them.
Refer to this link to learn about ASCII values: https://www.geeksforgeeks.org/ascii-table/
Java code
Here’s the complete code:
import java.util.Scanner; public class CharacterFrequency { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); System.out.println("Enter your String: "); String a=sc.nextLine(); int[] arr=new int[26]; a=a.toLowerCase(); for(int i=0;i<a.length();i++) { char b=a.charAt(i); if(Character.isLetter(b)) { arr[b-'a']++; } } for(char i='a';i<='z';i++) { if(arr[i-'a']>0) { System.out.println(i+":"+arr[i-'a']); } } } }
Let’s finally work out the code bit by bit!
import java.util.Scanner; public class CharacterFrequency { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); System.out.println("Enter your String: "); String a=sc.nextLine(); int[] arr=new int[26];
Firstly, we are taking input from the user. We use the ‘Scanner’ class to read the input string from the user. Then Declare the array of size 26.
a=a.toLowerCase(); for(int i=0;i<a.length();i++) { char b=a.charAt(i); if(Character.isLetter(b)) { arr[b-'a']++; } }
Here, we are converting the String to lowercase by using toLowerCase().
Then we initiated the for-loop which will run till we reach the end of the String we provided, thus iterating through the string and updating the frequency array based on the character’s ASCII value.
for(char i='a';i<='z';i++) { if(arr[i-'a']>0) { System.out.println(i+":"+arr[i-'a']); } } } }
Here we took a for-loop and initialized the ‘i’ value to the character ‘a’ running till it reached ‘z’. Then interating through the frequency array and print the characters with their respective frequencies.
Output:
Enter your String: CodeSpeedy c:1 d:2 e:3 o:1 p:1 s:1 y:1
That’s it!
In this tutorial, we learned how to write a Java Program to find the frequency of characters in strings. This program is useful in various text processing applications, such as analyzing a text’s letter distributions.
I hope you found this explanation helpful. Happy coding!