Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Uprajnenie;
- import java.util.Map;
- import java.util.Scanner;
- import java.util.TreeMap;
- public class CountSymbols {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- // Read input text
- String text = scanner.nextLine();
- // Create a TreeMap to store characters and their counts in alphabetical order
- Map<Character, Integer> charCounts = new TreeMap<>();
- // Count occurrences of each character (including spaces and punctuation)
- for (char c : text.toCharArray()) {
- charCounts.put(c, charCounts.getOrDefault(c, 0) + 1);
- }
- // Print the results in alphabetical order
- for (Map.Entry<Character, Integer> entry : charCounts.entrySet()) {
- char character = entry.getKey();
- int count = entry.getValue();
- String times = count == 1 ? "time" : "time";
- System.out.println(character + ": " + count + " " + times + "/s");
- }
- scanner.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement