Advertisement
deyanmalinov

01. Count Chars in a String

Mar 18th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.81 KB | None | 0 0
  1. package DPM;
  2.  
  3. import java.util.LinkedHashMap;
  4. import java.util.Map;
  5. import java.util.Scanner;
  6.  
  7. public class Main {
  8.     public static void main(String[] args) {
  9.         Scanner scan = new Scanner(System.in);
  10.         String line = scan.nextLine();
  11.  
  12.         Map<Character, Integer> lMap = new LinkedHashMap<>();
  13.  
  14.         for (int i = 0; i < line.length(); i++) {
  15.             char ch = line.charAt(i);
  16.             if (ch != ' '){
  17.                 if (lMap.containsKey(ch)){
  18.                     lMap.put(ch, lMap.get(ch)+1);
  19.                 }else {
  20.                     lMap.put(ch, 1);
  21.                 }
  22.             }
  23.  
  24.         }
  25.         for (Map.Entry<Character, Integer> entry : lMap.entrySet()) {
  26.  
  27.                 System.out.printf("%c -> %d%n", entry.getKey(), entry.getValue());
  28.  
  29.         }
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement