Java Program to Count Total Occurrence of Character in a String

Write a Java Program to Count Total Occurrence of Character in a String with an example. In this Java total occurrence of a string character example, we used the While loop to iterate the calStr string from start to end. Within the loop, we compare each calStr string character (charAt()) with ch. If they are equal, we incremented the charCount. Finally, we are printing the charCount as an output.

import java.util.Scanner;

public class CountAllCharOccurence1 {
	private static Scanner sc;
	public static void main(String[] args) {
		String calStr;
		char ch;
		int i = 0, charCount = 0;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter String to Count all Char Occurrence =  ");
		calStr = sc.nextLine();
		
		System.out.print("\nEnter the Character to Find =  ");
		ch = sc.next().charAt(0);
		
		while(i < calStr.length())
		{
			if(calStr.charAt(i) ==  ch) {
				charCount++;
			}
			i++;
		}
		System.out.format("\nTotal Number of time %c has found =  %d ", 
				ch, charCount);	
	}
}
Java Program to Count Total Occurrence of Character in a String 1

This Java count total occurrence of a character example is the same as the above, and we replaced the While loop with For Loop.

import java.util.Scanner;

public class CountAllCharOccurence2 {
	private static Scanner sc;
	public static void main(String[] args) {
		String calStr;
		char ch;
		int i, charCount = 0;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter String to Count all Char Occurrence =  ");
		calStr = sc.nextLine();
		
		System.out.print("\nEnter the Character to Find =  ");
		ch = sc.next().charAt(0);
		
		for(i = 0; i < calStr.length(); i++)
		{
			if(calStr.charAt(i) ==  ch) {
				charCount++;
			}
		}
		System.out.format("\nTotal Number of time %c has found =  %d ", 
				ch, charCount);	
	}
}
Please Enter String to Count all Char Occurrence =  hello world

Enter the Character to Find =  l

Total Number of time l has found =  3 

Java Program to Count Total Occurrence of Character in a String using Functions

Here, we created a CountAllCharOcc Java function that returns the character count.

import java.util.Scanner;

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

		System.out.print("\nPlease Enter String to Count all Char Occurrence =  ");
		String calStr = sc.nextLine();
		
		System.out.print("\nEnter the Character to Find =  ");
		char ch = sc.next().charAt(0);
		
		int charCount = CountAllCharOcc(calStr, ch);
		System.out.format("\nTotal Number of time %c has found =  %d ", ch, charCount);	
	}
	
	public static int CountAllCharOcc(String calStr, char ch) {
		int i, charCount = 0;
		
		for(i = 0; i < calStr.length(); i++)
		{
			if(calStr.charAt(i) ==  ch) {
				charCount++;
			}
		}
		return charCount;
	}
}
Please Enter String to Count all Char Occurrence =  tutorial gateway

Enter the Character to Find =  t

Total Number of time t has found =  3