desislava_topuzakova

04. Count Symbols

May 27th, 2021
1,196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Demo {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         String text = scanner.nextLine();
  7.         //символ -> брой срещанията
  8.         TreeMap <Character, Integer> symbols = new TreeMap<>();
  9.         for (int index = 0; index <= text.length() - 1; index++) {
  10.             char currentSymbol = text.charAt(index);
  11.             //преброим
  12.             //нямам такъв запазен символ
  13.             if(!symbols.containsKey(currentSymbol)) {
  14.                 symbols.put(currentSymbol, 1);
  15.             } else { //имам такъв запазен символ
  16.                 int currentCount = symbols.get(currentSymbol);
  17.                 symbols.put(currentSymbol, currentCount + 1);
  18.             }
  19.  
  20.         }
  21.  
  22.         // {symbol}: {count} time/s
  23.         for (Map.Entry<Character, Integer> entry: symbols.entrySet()) {
  24.             //символ -> брой на срещанията
  25.             System.out.println(entry.getKey() + ": " + entry.getValue() + " time/s");
  26.         }
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment