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
- Let a = “Java is awesome”
- Let counter = 0
- Let search_term = ‘a’
- Using for loop to iterate over each index of the string
- If any character is equivalent to our search character, we will increase our counter
- 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"); | |
} |
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
- Let a = “Java is awesome”
- 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.
- 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"); | |
} |
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
- Let a = “Java is awesome”
- Let search_term = ‘a’
- Subtract total length with the length without the search character using replace(character,””) method
- 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"); | |
} |
Total characters without spaces : 13