In this tutorial we will learn how to write program in Java to count occurrence of character in a String.
Also Read this: C program to count the occurrence of a character in String.
How this Java program will behave?
This Java program will take a string and a character as an input. And it will count the occurrence of a given character. This program will also count spaces I we will give it.
For example:
If we have given a String as an input “Quescol is educational website” and also a character ‘s’.
Then after counting character ‘a’, program will return output 2.
Java Program to Count Occurrence of given Character in String
Program 1: Using For Loop
This method directly iterates through each character in the string and increases a counter whenever it finds a match. This is a straightforward and intuitive approach, but it manually handles the traversal and comparison of each character.
import java.util.*;
public class CountOccurence {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.print("Please give a String : ");
String str= sc.nextLine();
Scanner sc1= new Scanner(System.in);
System.out.print("Please give a char to count occurence: ");
char ch= sc1.next().charAt(0);
System.out.print("Total Number of occurence of '"+ch+"' is: ");
System.out.println(countOccurence(str, ch));
}
public static int countOccurence(String str, char ch){
char arr[] = new char[str.length()];
int count=0;
for(int i=0; i<str.length();i++ ){
arr[i] = str.charAt(i);
if(arr[i]==ch){
count++;
}
}
return count;
}
}
Output:
Please give a String : quescol Please give a char to count occurence: e Total Number of occurence of 'e' is: 1
Program 2: Using Java 8 Streams
This method utilizes Java 8’s Stream API to create a stream of characters from the string and then applies a filter to count only the characters that match the given character. It’s a more functional programming approach and is very concise but might be less performant for small strings due to overhead of setting up the stream.
import java.util.*;
public class CountOccurence {
public static long countChar(String str, char ch) {
return str.chars()
.filter(c -> c == ch)
.count();
}
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.print("Please give a String : ");
String str= sc.nextLine();
Scanner sc1= new Scanner(System.in);
System.out.print("Please give a char to count occurrence: ");
char ch= sc1.next().charAt(0);
System.out.print("Total Number of occurrence of '"+ch+"' is: ");
System.out.println(countChar(str, ch));
}
}
Output
Please give a String : quescol Please give a char to count occurrence: u Total Number of occurrence of 'u' is: 1
Program 3: Using a Regular Expression
This method involves replacing all occurrences of the character with an empty string and then comparing the length of the modified string with the original. The difference gives the number of occurrences. This method is simple but may not be very efficient for large strings or when performance is a critical concern.
import java.util.*;
public class CountOccurence {
public static int countChar(String str, char ch) {
return str.length() - str.replaceAll(String.valueOf(ch), "").length();
}
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.print("Please give a String : ");
String str= sc.nextLine();
Scanner sc1= new Scanner(System.in);
System.out.print("Please give a char to count occurrence: ");
char ch= sc1.next().charAt(0);
System.out.print("Total Number of occurrenceof '"+ch+"' is: ");
System.out.println(countChar(str, ch));
}
}
Output
Please give a String : quescolques Please give a char to count occurrence: e Total Number of occurrence of 'e' is: 2