Introduction
In Java, we can easily count the number of times a specific character occurs in a string. This lab will guide you through different methods you can use to count the occurrences of a character in a string.
In Java, we can easily count the number of times a specific character occurs in a string. This lab will guide you through different methods you can use to count the occurrences of a character in a string.
We can use an iterative approach to traverse through each character in a string to count the number of times a specific character occurs.
Create a Java file named CountOccurrences.java
in the ~/project
directory, and copy the following contents to the file:
public class CountOccurrences {
public static int countChars(String str, char c) {
int count = 0;
for(int i = 0; i < str.length(); i++) {
char currChar = str.charAt(i);
if(currChar == c)
count += 1;
}
return count;
}
public static void main(String[] args) {
String s = "Java is an awesome language!";
int charCountOfA = countChars(s, 'a');
int charCountOfG = countChars(s, 'g');
int charCountOfE = countChars(s, 'e');
System.out.println("The String is: " + s);
System.out.println("Character count of 'a': " + charCountOfA);
System.out.println("Character count of 'g': " + charCountOfG);
System.out.println("Character count of 'e': " + charCountOfE);
}
}
To compile and run the program, run the following commands in the terminal:
cd ~/project
javac CountOccurrences.java && java CountOccurrences
The output should be:
The String is: Java is an awesome language!
Character count of 'a': 6
Character count of 'g': 2
Character count of 'e': 3
We can also use a recursive approach to count the occurrences of a character in a string. This involves using two methods, with the first being recursive and the second one invoking the first.
Copy and paste the following code block after Step 1 code block in the CountOccurrences.java
file:
public static int countCharsRecur(String str, char c, int idx) {
if(idx >= str.length())
return 0;
else {
int count = 0;
if(str.charAt(idx) == c)
count = 1;
return count + countCharsRecur(str, c, idx + 1);
}
}
public static int countChars(String s, char c) {
return countCharsRecur(s, c, 0);
}
To compile and run the updated program, run the same commands as in Step 1:
cd ~/project
javac CountOccurrences.java && java CountOccurrences
We can use Java 8 Streams to count the occurrences of a character in a string.
Copy and paste the following code block after Step 2 code block in the CountOccurrences.java
file:
public static void usingStreams(String s) {
int charCountOfA = (int) s.chars().filter(c -> c == 'a').count();
int charCountOfG = (int) s.chars().filter(c -> c == 'g').count();
int charCountOfE = (int) s.chars().filter(c -> c == 'e').count();
System.out.println("The String is: " + s);
System.out.println("Character count of 'a': " + charCountOfA);
System.out.println("Character count of 'g': " + charCountOfG);
System.out.println("Character count of 'e': " + charCountOfE);
}
To use the Java 8 Streams method, add the following line of code to the main
method after the Step 2 code block:
usingStreams(s);
To compile and run the updated program, run the same commands as in Step 1:
cd ~/project
javac CountOccurrences.java && java CountOccurrences
We can use external libraries to count the occurrences of a character in a string.
The Guava library provides the CharMatcher
class that can count the number of occurrences of a given character.
Add the following code block after the Step 3 code block:
public static int usingGuava(String str) {
CharMatcher cm = CharMatcher.is('a');
int charCountOfA = cm.countIn(str);
System.out.println("Character count of 'a': " + charCountOfA);
return charCountOfA;
}
To use the Guava library, add the following imports to the top of the CountOccurrences.java
file:
import com.google.common.base.CharMatcher;
In the main
method, call the usingGuava
method with the string parameter:
usingGuava(s);
To compile and run the updated program, run the same commands as in Step 1:
cd ~/project
javac -cp ".:./lib/*" CountOccurrences.java && java -cp ".:./lib/*" CountOccurrences
The Apache Commons library provides a StringUtils
class that has a convenient countMatches()
method that takes a char and a string as input and returns the count of the character in that String as output.
Add the following code block after the Step 4 code block:
public static int usingApache(String str) {
int charCountOfA = StringUtils.countMatches(str, 'a');
int charCountOfG = StringUtils.countMatches(str, 'g');
int charCountOfE = StringUtils.countMatches(str, 'e');
System.out.println("Character count of 'a': " + charCountOfA);
System.out.println("Character count of 'g': " + charCountOfG);
System.out.println("Character count of 'e': " + charCountOfE);
return charCountOfA;
}
To use the Apache Commons library, add the following import to the top of the CountOccurrences.java
file:
import org.apache.commons.lang3.StringUtils;
In the main
method, call the usingApache
method with the string parameter:
usingApache(s);
To compile and run the updated program, run the same commands as in Step 1, with an added classpath:
cd ~/project
javac -cp ".:./lib/*" CountOccurrences.java && java -cp ".:./lib/*" CountOccurrences
In this lab, we covered the following steps:
After completing this lab, you should have a better understanding of different methods to count occurrences of a character in a string.