🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
Counting the occurrences of each character in a string is a common problem in text processing. This task is useful in various applications such as text analysis, frequency analysis, and cryptography. In this blog post, we will explore how to write a Java program to count the occurrences of each character in a given string.
Approach
To count the occurrences of each character in a string, we can use a HashMap to store the characters as keys and their counts as values. The program will iterate through each character in the string, update the count in the map, and finally, print the results.
Example Program
Example Code:
import java.util.HashMap;
import java.util.Map;
public class CharacterCount {
public static void main(String[] args) {
String input = "Java is great and Java is fun.";
// Call the method to count character occurrences
Map<Character, Integer> characterCountMap = countCharacterOccurrences(input);
// Print the character count
System.out.println("Character occurrences in the string:");
for (Map.Entry<Character, Integer> entry : characterCountMap.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
// Method to count occurrences of each character in a string
public static Map<Character, Integer> countCharacterOccurrences(String input) {
// Create a HashMap to store character counts
Map<Character, Integer> characterCountMap = new HashMap<>();
// Convert the string to a character array
char[] characters = input.toCharArray();
// Iterate through each character in the string
for (char c : characters) {
if (characterCountMap.containsKey(c)) {
// If the character is already in the map, increment its count
characterCountMap.put(c, characterCountMap.get(c) + 1);
} else {
// If the character is not in the map, add it with a count of 1
characterCountMap.put(c, 1);
}
}
return characterCountMap;
}
}
Output:
Character occurrences in the string:
=6
.:1
a=6
d=1
e=2
f=1
g=2
i=2
J=2
n=2
r=1
s=2
t=1
u=1
v=2
Explanation:
-
CharacterCount Class:
- The
CharacterCountclass contains themainmethod and a helper methodcountCharacterOccurrences.
- The
-
countCharacterOccurrences Method:
- This method takes a string as input and returns a
Mapcontaining each character and its count. - A
HashMapis used to store the characters as keys and their counts as values. - The input string is converted to a character array using
toCharArray(). - The method iterates through each character in the array. If the character is already in the map, its count is incremented. Otherwise, the character is added to the map with a count of 1.
- This method takes a string as input and returns a
-
Main Method:
- The
mainmethod defines the input string and calls thecountCharacterOccurrencesmethod. - The results are printed by iterating through the entries in the map.
- The
Conclusion
This Java program efficiently counts the occurrences of each character in a string using a HashMap. This approach ensures that each character is processed and counted accurately, making it suitable for various text-processing applications. Understanding and implementing this method will help you handle character frequency analysis and similar tasks effectively in your Java projects.
Happy coding!
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment