Advertisement
grubcho

Letter repetition - Dictionaries

Jul 10th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.94 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. //You will be given a single string, containing random ASCII character.Your task is to print every character, and how many times it has //been used in the string.
  7. namespace Letter_Repetition
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Dictionary<char, int> letters = new Dictionary<char, int>();
  14.             string words = Console.ReadLine();
  15.  
  16.             for (int i = 0; i < words.Length; i++)
  17.             {
  18.                 if (!letters.ContainsKey(words[i]))
  19.                 {
  20.                     letters.Add(words[i], 0);
  21.                 }
  22.                 letters[words[i]]++;
  23.             }
  24.             foreach (KeyValuePair<char, int> letter in letters)
  25.             {
  26.                 Console.WriteLine($"{letter.Key} -> {letter.Value}");
  27.             }
  28.         }
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement