Advertisement
Krassi_Daskalova

CountCharsInAString

Aug 2nd, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. import java.util.LinkedHashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4.  
  5. public class CountCharsInAString1 {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8. String text = scanner.nextLine();
  9. Map<Character, Integer> count = new LinkedHashMap<>();
  10.  
  11. for (char c : text.toLowerCase().toCharArray()) {
  12. if (c != ' ') {
  13. if (!count.containsKey(c)) {
  14. count.put(c, 1);
  15. } else {
  16. count.put(c, count.get(c) + 1);
  17. }
  18. }
  19. }
  20. for (Map.Entry<Character, Integer> entry : count.entrySet()) {
  21. System.out.printf("%c -> %d%n", entry.getKey(), entry.getValue());
  22. }
  23. }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement