StoimenK

C# 2.1.Count_Chars_in_a_String

Mar 13th, 2020
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace _2._1.Count_Chars_in_a_String
  5. {
  6.     class Program
  7.     {
  8.         static void Main()
  9.         {
  10.             string word = Console.ReadLine();
  11.  
  12.             Dictionary<char, int> histogram = new Dictionary<char, int>();
  13.  
  14.             foreach (var currentChar in word)
  15.             {
  16.                 if (currentChar != ' ')
  17.                 {
  18.                     if (!histogram.ContainsKey(currentChar))
  19.                     {
  20.                         // histogram.Add(currentChar, 0);
  21.                         histogram[currentChar] = 0;
  22.                     }
  23.                     histogram[currentChar]++;
  24.                 }
  25.             }
  26.  
  27.             foreach (var item in histogram)
  28.             {
  29.                 Console.WriteLine($"{item.Key} -> {item.Value}");
  30.             }
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment