Java program to count the occurrences of each character
This article gives several techniques to figure out the occurrence of each character in a Java string. These techniques find out how many times a given character is repeated in a given string.
Java program to count the occurrences of each character using counter array
In the given program, we have used the counter array to count the occurrences of each character in a string. For this, we have used the for loop and iterated over the given string, and increased the count by 1 at each index based on character. Again, we have iterated over the counter array and printed character and frequency if counter[i] is not 0.
// Java Program to
// count the occurrences of each character
import java.util.Scanner;
public class EachCharacterCount {
public static void main(String args[]) {
String str;
int i, len, counter[] = new int[256];
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter a string : ");
str = scanner.nextLine();
//String length
len = str.length();
// loop through the string
// and count frequency of every character
// and store it in counter array
for (i = 0; i < len; i++) {
counter[(int) str.charAt(i)]++;
}
// Print Frequency of characters
for (i = 0; i < 256; i++) {
if (counter[i] != 0) {
System.out.println((char) i + " --> " + counter[i]);
}
}
}
}
Output of the above code:
Please enter a string : etutttoriaalspooint
a --> 2
e --> 1
i --> 2
l --> 1
n --> 1
o --> 3
p --> 1
r --> 1
s --> 1
t --> 5
u --> 1
Java program to count the occurrences of each character using HashMap
To find the number of occurrences of each character in a given string, we have used HashMap with a character as a key and its occurrences as a value. First, we convert the given string to a character array and check each character one by one. And update its count in the HashMap. For every character, you need to verify if the key already exists in the HashMap. If it does, increase the count, otherwise; add it to the map as a new key and provide an initial value with count 1.
import java.util.HashMap;
public class EachCharacterCount {
public static void main(String[] args) {
String str = "ettutoriaalsspoint";
// HashMap char as a key and occurrence as a value
HashMap <Character, Integer> charCount = new HashMap<>();
for (int i = str.length() - 1; i >= 0; i--) {
if(charCount.containsKey(str.charAt(i))) {
int count = charCount.get(str.charAt(i));
charCount.put(str.charAt(i), ++count);
} else {
charCount.put(str.charAt(i),1);
}
}
System.out.println(charCount);
}
}
Output of the above code:
{p=1, a=2, r=1, s=2, t=4, u=1, e=1, i=2, l=1, n=1, o=2}
Java program to count the occurrences of each character using char array and string replace() method
In the given Java program, we have used the char array and string replace() method to count the frequency of each character in a given string.
public class CountChars {
public static void main(String[] args) {
//input String
CountChars.eachCharacterCount ("eetutorialssspointtt");
}
public static void eachCharacterCount (String str){
char[] stringArray;
do{
stringArray = str.toCharArray();
char chr = stringArray[0];
int count = 1;
for(int i = 1; i < stringArray.length; i++){
if(chr == stringArray[i]){
count++;
}
}
// remove count for space or tab
if(((chr != ' ') && (chr != '\t'))){
System.out.println(chr + " - " + count);
}
str = str.replace(""+chr, "");
// terminate the loop
// if string is of length 0
if(str.length() == 0) {
break;
}
}while(stringArray.length > 1);
}
}
Output of the above code:
Output:
e - 2
t - 5
u - 1
o - 2
r - 1
i - 2
a - 1
l - 1
s - 3
p - 1
n - 1
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java