Advertisement
Asenval

Untitled

Feb 1st, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class CountEachLetterInString
  5. {
  6.     static void Main()
  7.     {
  8.         //string str = "Write a program that reads a string from the console and prints all different letters, in the string along with information how many times each letter is found.";
  9.         Console.WriteLine("Insert a string");
  10.         string str = Console.ReadLine();
  11.         Dictionary<char, int> Letters = new Dictionary<char, int>();
  12.         foreach (var item in str.ToLower())
  13.         {
  14.             if (Char.IsLetter(item))
  15.             {
  16.                 if (Letters.ContainsKey(item))
  17.                 {
  18.                     Letters[item]++;
  19.                 }
  20.                 else
  21.                 {
  22.                     Letters.Add(item, 1);
  23.                 }
  24.             }
  25.         }
  26.  
  27.         foreach (var letter in Letters)
  28.         {
  29.             Console.WriteLine("{0} - {1,3} times found",letter.Key,letter.Value);
  30.         }
  31.  
  32.         Console.WriteLine();
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement