Teo_Yanchev

Dictionaries - 01. CountCharInAString - C# Fundamentals

Oct 4th, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Exercise_AssociativeArrays___Dictionaries__
  6. {
  7. class CountCharInAString
  8. {
  9. static void Main()
  10. {
  11. Dictionary<char, int> dictLetters = new Dictionary<char, int>();
  12.  
  13. string command = Console.ReadLine();
  14. for (int i = 0; i < command.Length; i++)
  15. {
  16. char letter = command[i];
  17.  
  18. if (letter != ' ')
  19. {
  20. if (!dictLetters.ContainsKey(letter))
  21. {
  22. dictLetters[letter] = 0;
  23. }
  24.  
  25. dictLetters[letter] += 1;
  26. }
  27. }
  28.  
  29. foreach (var letter in dictLetters)
  30. {
  31. Console.WriteLine($"{letter.Key} -> {letter.Value}");
  32. }
  33. }
  34. }
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment