Java Program to Count Total Occurrence of Character in a String

Write a Java Program to Count the Total Occurrence of Character in a String with an example. In this 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 letter (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

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

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

import java.util.Scanner;

public class CntAllChOcc32 {
	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 =  ");
		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 =  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 function that returns the character count.

import java.util.Scanner;

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

		System.out.print("\nPlease Enter Text =  ");
		String calStr = sc.nextLine();
		
		System.out.print("\nEnter the Letter to Find =  ");
		char ch = sc.next().charAt(0);
		
		int charCount = CntAllCharOcc(calStr, ch);
		System.out.format("\nTotal Number of time %c has found =  %d ", ch, charCount);	
	}
	
	public static int CntAllCharOcc(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 Text =  tutorial gateway

Enter the letter to Find =  t

Total Number of time t has found =  3 

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.