🎓 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 task in text processing and analysis. This guide will show you how to create a Java program that counts and displays the frequency of each character in a given string.
Problem Statement
Create a Java program that:
- Takes a string as input.
- Counts and displays the frequency of each character in the string.
Example 1:
- Input:
"hello" - Output:
h: 1, e: 1, l: 2, o: 1
Example 2:
- Input:
"programming" - Output:
p: 1, r: 2, o: 1, g: 2, a: 1, m: 2, i: 1, n: 1
Solution Steps
- Prompt for Input: Use the
Scannerclass to read a string input from the user. - Use a
HashMapto Track Character Frequencies: Iterate through the string and store the frequency of each character in aHashMap. - Display the Character Frequencies: Print the frequency of each character in the string.
Java Program
Java Program to Count the Occurrences of Each Character
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/**
* Java Program to Count the Occurrences of Each Character in a String
* Author: https://www.javaguides.net/
*/
public class CharacterFrequencyCounter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Step 1: Prompt the user for input
System.out.print("Enter a string: ");
String input = scanner.nextLine();
// Step 2: Use a HashMap to track character frequencies
Map<Character, Integer> charCountMap = new HashMap<>();
for (char c : input.toCharArray()) {
charCountMap.put(c, charCountMap.getOrDefault(c, 0) + 1);
}
// Step 3: Display the character frequencies
System.out.println("Character frequencies:");
for (Map.Entry<Character, Integer> entry : charCountMap.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
Explanation
- Input: The program prompts the user to enter a string.
- Tracking Character Frequencies: The program uses a
HashMapto count the occurrences of each character in the string. ThegetOrDefaultmethod is used to increment the count if the character already exists in the map or to initialize it if it does not. - Displaying Frequencies: The program then iterates over the
HashMapto display each character and its corresponding frequency.
Output Example
Example 1:
Enter a string: hello
Character frequencies:
h: 1
e: 1
l: 2
o: 1
Example 2:
Enter a string: programming
Character frequencies:
p: 1
r: 2
o: 1
g: 2
a: 1
m: 2
i: 1
n: 1
Example 3:
Enter a string: javaguides
Character frequencies:
j: 1
a: 2
v: 1
g: 1
u: 1
i: 1
d: 1
e: 1
s: 1
Conclusion
This Java program effectively counts the occurrences of each character in a string using a HashMap. By tracking the frequency of each character, the program can display the exact number of times each character appears in the input string. This approach is practical for various text processing tasks, such as analyzing text or ensuring data integrity.
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