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
Similar Reads
-
Find middle element of a linked list in single pass
Linked lists are a fundamental data structure used in computer science for organizing and managing data. A linked list is… -
Python Program to Find Last 3rd element in Singly Linked List
In this tutorial, we are going to learn the writing python program to Find 3rd element of Linked List from… -
Most important JavaScript Interview Questions To Prepare
In this Page we have collected and explained Most important Javascript Interview Questions and Answers for begineers, freshers as well… -
Sum of digits of Given Number in Java
In this tutorial we will learn writing Java program to calculate the sum of its digit. We will also see… -
Hibernate Interview Questions for 2+ years of experience
Certainly! Here's a list of commonly asked interview questions on Hibernate for candidates with 2+ years of experience: Basic Hibernate… -
68 Most Important Microservices Interview Questions
Certainly, here's an extended list of 50 commonly asked interview questions on microservices for candidates with 2+ years of experience:… -
60 Most Important Git Interview Questions
Certainly! Here is a list of commonly asked interview questions on Git for candidates with fresher or having of experience… -
50+ Most important Java Interview Questions for 5+ Years Exp
1. Explain the SOLID principles in Java. Provide examples of how you have applied these principles in your projects. SOLID… -
60+ Spring Boot interview questions for 4+ years Exp.
1. What is Spring Boot and how does it differ from the Spring framework? Spring Boot is a framework designed… -
60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
Here is a list of 60+ Spring Boot interview questions for candidates with 3+ years of experience: 1. What is…