Understanding the frequency of characters in a text can be essential for various reasons, such as text analysis, cryptography, or even just for fun. In this article, we'll walk beginners through creating a Java program that counts the occurrences of each character in a given string.
Overview
The idea is simple. We will go through each character in the string and keep a count of how many times each character appears.
We use HashMap to store the character along with it's count.
Java Program to Count the Occurrences of Each Character
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class CharacterCount {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the string:");
String input = scanner.nextLine();
Map<Character, Integer> charCount = countCharacters(input);
System.out.println("Occurrences of each character:");
for (Map.Entry<Character, Integer> entry : charCount.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
public static Map<Character, Integer> countCharacters(String input) {
Map<Character, Integer> charCountMap = new HashMap<>();
for (char c : input.toCharArray()) {
charCountMap.put(c, charCountMap.getOrDefault(c, 0) + 1);
}
return charCountMap;
}
}
Output:
Enter the string:
hello
Occurrences of each character:
h: 1
e: 1
l: 2
o: 1
Step by Step Explanation:
Setting up the Input:
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the string:");
String input = scanner.nextLine();
Using the Scanner class, we take the string input from the user.
Using a HashMap:
Map<Character, Integer> charCount = countCharacters(input);
We employ a HashMap where the key is the character from the string and the value is the count of that character. The HashMap allows for quick look-ups and insertions.
Counting Characters:
for (char c : input.toCharArray()) {
charCountMap.put(c, charCountMap.getOrDefault(c, 0) + 1);
}
We convert the string to a character array and iterate through each character. For every character, we update its count in our HashMap. If the character doesn't exist, we use a default value of 0 and add 1 to it.
Displaying the Results:
System.out.println("Occurrences of each character:");
for (Map.Entry<Character, Integer> entry : charCount.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
We iterate through the entries of our HashMap and print out the character and its corresponding count.
Conclusion
Counting the occurrences of characters in a string is a foundational exercise in understanding data structures like HashMap and text manipulation in Java. In this article, we explored how to write a Java program to count the occurrences of each character.
Related Java String Programs with Output
- Java Program to Find the First Non-repeated Character in a String
- Java Program to Check Palindrome String
- Java Program to Find Duplicate Characters in a String
- Java Program to Find Duplicate Words in a String
- Java Program to Find All the Permutations of a String
- Java Program to Count Occurrences of Words in a String
- Java Program to Count the Occurrences of Each Character
- Java Program to Count Vowels and Consonants in a String
- Java program to Count the Number of Duplicate Words in a String
- Java Program to Count Number of Words in Given String
- Java Program to Count the Number of Occurrences of Substring in a String
- Java Program to Count the Occurrences of Each Character in String
- Java Program to Merge Two String Arrays
- Java Program to Remove Duplicate Words from String
- Java Program to Reverse a String(5 ways)
- Java Program to Reverse Each Word of a String
- Java Program to Swap Two Strings
- How to Check if the String Contains Only Digits
- How to Check if the String Contains Only Letters
- How to Check If the String Contains Only Letters or Digits
Comments
Post a Comment
Leave Comment