Java Program to Find Frequency of each Character in a String

Write a Java Program to Find the Frequency of each Character in a String using a while loop with an example. First, we declared the 256 size charFreq integer array. Next, we traversed the freqStr string and assigned character frequency to the charFreq array. Then, we used another while loop to return the output.

import java.util.Scanner;

public class FreqOfEachChar {
	private static Scanner sc;
	public static void main(String[] args) {
		String freqStr;
		int i = 0, j = 0;
		int[] charFreq = new int[256];
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter String to find Frequency of each Char =  ");
		freqStr = sc.nextLine();
		
		while(i < freqStr.length())
		{
			charFreq[freqStr.charAt(i)]++;
			i++;
		}
		while(j < 256) {
			if(charFreq[j] != 0) {
				System.out.format("\n'%c' Character Occurs %d Times ", j, charFreq[j]);	
			}
			j++;
		}
	}
}
Java Program to Find Frequency of each Character in a String

Java Program to Find Frequency of each Character in a String using For Loop

import java.util.Scanner;

public class FreqOfEachChar2 {
	private static Scanner sc;
	public static void main(String[] args) {
		String freqStr;
		int i;
		int[] charFreq = new int[256];
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter String =  ");
		freqStr = sc.nextLine();
		
		for(i = 0; i < freqStr.length(); i++)
		{
			charFreq[freqStr.charAt(i)]++;
		}
		for(i = 0; i < 256; i++) {
			if(charFreq[i] != 0) {
				System.out.format("\n'%c' Character Occurs %d Times ", i, charFreq[i]);	
			}
		}
	}
}
Please Enter String =  java program

' ' Character Occurs 1 Times 
'a' Character Occurs 3 Times 
'g' Character Occurs 1 Times 
'j' Character Occurs 1 Times 
'm' Character Occurs 1 Times 
'o' Character Occurs 1 Times 
'p' Character Occurs 1 Times 
'r' Character Occurs 2 Times 
'v' Character Occurs 1 Times 

It is another way to write a Program to return each character frequency in a String.

import java.util.Scanner;

public class FreqOfEachChar3 {
	private static Scanner sc;
	public static void main(String[] args) {
		String freqStr;
		int i;
		int[] charFreq = new int[256];
		
		sc= new Scanner(System.in);

		System.out.print("\nEnter String to find Occurrence of each Char =  ");
		freqStr = sc.nextLine();
		
		for(i = 0; i < freqStr.length(); i++)
		{
			char ch = freqStr.charAt(i);
			if(ch >= 'a' && ch <= 'z') {
				charFreq[ch - 'a']++;
			}
			else if(ch >= 'A' && ch <= 'Z') {
				charFreq[ch - 'A']++;
			}
		}
		for(i = 0; i < 256; i++) {
			if(charFreq[i] != 0) {
				System.out.format("\n'%c' Occurs %d Times ", i + 'a', charFreq[i]);	
			}
		}
	}
}
Enter String to find Occurrence of each Char =  hello world

'd' Occurs 1 Times 
'e' Occurs 1 Times 
'h' Occurs 1 Times 
'l' Occurs 3 Times 
'o' Occurs 2 Times 
'r' Occurs 1 Times 
'w' Occurs 1 Times 

This example code finds string character frequency is the same as the above. Here, we separated the logic using Java functions.

import java.util.Scanner;

public class FreqOfEachChar4 {
	private static Scanner sc;
	public static void main(String[] args) {
		String freqStr;
	
		sc= new Scanner(System.in);

		System.out.print("\nEnter Any Text =  ");
		freqStr = sc.nextLine();
		
		FreqOfEachChar(freqStr);		
	}
	
	public static void FreqOfEachChar(String freqStr) {
		int i;
		int[] charFreq = new int[256];
		
		for(i = 0; i < freqStr.length(); i++)
		{
			charFreq[freqStr.charAt(i)]++;
		}
		for(i = 0; i < 256; i++) {
			if(charFreq[i] != 0) {
				System.out.format("\n'%c' Character Occurs %d Times ", i, charFreq[i]);	
			}
		}
	}
}

Enter Any Text =  tutorial gateway

' ' Character Occurs 1 Times 
'a' Character Occurs 3 Times 
'e' Character Occurs 1 Times 
'g' Character Occurs 1 Times 
'i' Character Occurs 1 Times 
'l' Character Occurs 1 Times 
'o' Character Occurs 1 Times 
'r' Character Occurs 1 Times 
't' Character Occurs 3 Times 
'u' Character Occurs 1 Times 
'w' Character Occurs 1 Times 
'y' Character Occurs 1 Times 

Please refer to the following string programs.

  1. Count the Total Occurrence of a Character in a String
  2. Find All Occurrences of a Character in a String
  3. First Character Occurrence in a String
  4. Replace First Character Occurrence in a String
  5. Remove First Character Occurrence in a String
  6. Last Character Occurrence in a String
  7. Replace Last Character Occurrence in a String
  8. Remove the Last Character Occurrence in a String
  9. First and Last Character in a String
  10. Remove The First and Last Character in a String
  11. Maximum Occurring Character in a String
  12. Minimum Occurring Character in a String
  13. Remove All Occurrences of a Character in a String