Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Scanner;
- public class CountCharsInAString {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String sequence = scanner.nextLine();
- Map<Character, Integer> counts = new HashMap<>();
- String sequenceWithoutSpaces = sequence.replaceAll("\\s+", "");
- for (int i = 0; i < sequenceWithoutSpaces.length(); i++) {
- char currentChar = sequenceWithoutSpaces.charAt(i);
- if (!counts.containsKey(currentChar)) {
- counts.put(currentChar, 1);
- } else {
- int currentCount = counts.get(currentChar);
- counts.put(currentChar, currentCount + 1);
- }
- }
- for (Map.Entry<Character, Integer> entry : counts.entrySet()) {
- System.out.println(entry.getKey() + " -> " + entry.getValue());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement