Java Program to Find Frequency of each Character in a String

Write a Java Program to Find 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 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 1

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 Java 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 Java code to find 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