SHARE
TWEET

02.8.21.LetterCount

stanevplamen Jun 13th, 2013 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class LettersCountDict
  5. {
  6.     static string someText = "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.".ToLower();
  7.  
  8.     static Dictionary<char, int> LettersCount()
  9.     {
  10.         Dictionary<char, int> letters = new Dictionary<char, int>();
  11.  
  12.         for (int i = 'a'; i <= 'z'; i++)
  13.         {
  14.             letters.Add((char)i, 0);
  15.         }
  16.  
  17.         foreach (char c in someText)
  18.         {
  19.             if (letters.ContainsKey(c))
  20.             {
  21.                 letters[c]++;
  22.             }
  23.         }
  24.         return letters;
  25.     }
  26.  
  27.     static void Main()
  28.     {
  29.         Dictionary<char, int> dotNetWords = LettersCount();
  30.  
  31.         foreach (KeyValuePair<char, int> kvp in dotNetWords)
  32.         {
  33.             if ((kvp.Value != 0) == true)
  34.             {
  35.                 string answer = string.Format("{0} - {1}", kvp.Key, kvp.Value);
  36.                 Console.WriteLine(answer);
  37.             }
  38.         }
  39.     }
  40. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top