Important Java Concept

Count occurrences of character in string in Java

Welcome readers,
There are many ways by which you can count occurrences of character in string in java.
Let’s go over them in detail.
Let’s understand the problem by seeing one test case
 
Example 1:

Input: term = "Java is awesome"
search_term = 'a' Output: Character 'a' occured 3 times
 

1. Using charAt() to count occurrences of character in string in java

 

This is the first approach, we are going to use to count the occurrence of a character in a String.
Here we are going to iterate over the string and check if any character is equal to our search character.
Whenever we find a character equivalent to our search character, we will increase our counter.

Request: Before reading the code, read the algorithm and try on your IDE.

 

Algorithm

 

  1. Let  a = “Java is awesome”
  2. Let counter = 0
  3. Let search_term = ‘a’
  4. Using for loop to iterate over each index of the string
  5. If any character is equivalent to our search character, we will increase our counter
  6. printing the counter

 

Code

 

public static void main(String[] args) {
String term = "Java is awesome";
char search_term = 'a';
int counter = 0;
for (int i = 0; i < term.length(); i++) {
if (term.charAt(i) == search_term) {
counter++;
}
}
System.out.println("Character '" + search_term + "' occured " + counter + " times");
}

 

OUTPUT:
Character 'a' occured 3 times

 

2. Using Java 8 streams to count occurrences of character in string in java

 

This is the second method used for counting the occurrence of a character in a String.
We will be using the new Java 8 Streams in this method.

Request: Before reading the code, read the algorithm and try on your IDE.

 

Algorithm

 

  1. Let  a = “Java is awesome”
  2. We’ll put variable count equal to input.chars().filter(ch -> ch==search_term).count() which is going over each character and checking if it’s equivalent to the seach character or not, and givs us total characters which matched.
  3. Print the count

 

Code

 

public static void main(String[] args) {
String term = "Java is awesome";
char search_term = 'a';
int counter = (int) term.chars().filter(ch -> ch==search_term).count();
System.out.println("Character '" + search_term + "' occured " + counter + " times");
}

 

OUTPUT:
  Total characters without spaces : 13

 

3. Using replace() to count occurrences of character in string in java

 

This is the shortcut to find the total count of characters in a String.
We will be using the basic .length() method in order to get the total length.
Then, we will use replace(character,””) method to get the length without the search character.

In order to count occurrences, we will subtract total length with the length without the search character.

Request: Before reading the code, read the algorithm and try on your IDE.

 

Algorithm

 

  1. Let  a = “Java is awesome”
  2. Let search_term = ‘a’
  3. Subtract total length with the length without the search character using replace(character,””) method 
  4. Printing the count 

 

Code

 

public static void main(String[] args) {
String term = "Java is awesome";
char search_term = 'a';
int counter = term.length() - term.replace(String.valueOf(search_term), "").length();
System.out.println("Character '" + search_term + "' occured " + counter + " times");
}

 

OUTPUT:
  Total characters without spaces : 13

 

In this java tutorial, we learned how to write a Java Program to count occurrences of character in string in Java.
I hope this article helped you.
 
In case the solution is not clear to you, I suggest you take a live 1:1 lesson on this subject from me. I am an Oracle-certified tutor who would love to teach this stuff to you.
Aniket Malik

Do you want

Java Tutor?

Aniket Malik

Aniket Malik

ORACLE CERTIFIED JAVA TUTOR

🟢 Oracle Certified Java Expert

🟢 300 + ⭐ ⭐ ⭐ ⭐ ⭐

🟢 B.tech & M.tech in CSE

🟢 6+ Years of Teaching Experience

🟢 Worked as SE in Virtusa and Digidez

Do you want

Java Tutor?