Introduction
Finding the maximum occurring character in a string is a common problem in text processing. This task helps you understand how to count the frequency of characters and determine which character appears the most. This guide will walk you through writing a Java program that identifies the character with the highest frequency in a given string.
Problem Statement
Create a Java program that:
- Prompts the user to enter a string.
- Identifies and counts the occurrences of each character in the string.
- Determines the character that occurs the most.
- Displays the maximum occurring character along with its count.
Example:
- Input:
"programming" - Output:
Maximum occurring character: g Occurrences: 2
Solution Steps
- Read the String: Use the
Scannerclass to take the string as input from the user. - Initialize a Character Frequency Map: Use a
HashMapto store each character and its frequency. - Count the Frequency of Each Character: Iterate through the string and update the frequency map.
- Determine the Maximum Occurring Character: Iterate through the map to find the character with the highest frequency.
- Display the Result: Print the character with the maximum frequency and its count.
Java Program
// Java Program to Find Maximum Occurring Character in a String
// Author: https://www.rameshfadatare.com/
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class MaxOccurringCharacter {
public static void main(String[] args) {
// Step 1: Read the string from the user
try (Scanner scanner = new Scanner(System.in)) {
System.out.print("Enter a string: ");
String input = scanner.nextLine();
// Step 2: Initialize the character frequency map
Map<Character, Integer> charCountMap = new HashMap<>();
// Step 3: Count the frequency of each character
for (char ch : input.toCharArray()) {
charCountMap.put(ch, charCountMap.getOrDefault(ch, 0) + 1);
}
// Step 4: Determine the maximum occurring character
char maxChar = ' ';
int maxCount = 0;
for (Map.Entry<Character, Integer> entry : charCountMap.entrySet()) {
if (entry.getValue() > maxCount) {
maxChar = entry.getKey();
maxCount = entry.getValue();
}
}
// Step 5: Display the result
System.out.println("Maximum occurring character: " + maxChar);
System.out.println("Occurrences: " + maxCount);
}
}
}
Explanation
Step 1: Read the String
- The
Scannerclass is used to read a string input from the user. ThenextLine()method captures the entire line as a string.
Step 2: Initialize the Character Frequency Map
- A
HashMapis used to store each character as a key and its count as the value.
Step 3: Count the Frequency of Each Character
- A
forloop iterates over each character in the string, and thegetOrDefault()method is used to increment the count for each character in the map.
Step 4: Determine the Maximum Occurring Character
- The program iterates through the
HashMapentries to find the character with the highest frequency. If a character’s count is greater than the current maximum count, it updates the maximum character and count.
Step 5: Display the Result
- The program prints the character that occurs the most in the string and the number of times it appears.
Output Example
Example:
Enter a string: programming
Maximum occurring character: g
Occurrences: 2
Conclusion
This Java program demonstrates how to find and display the maximum occurring character in a user-input string. It covers essential concepts such as string manipulation, using maps to store character counts, and iterating through collections, making it a valuable exercise for beginners learning Java programming.