Advertisement
dimipan80

Count Symbols

May 10th, 2015
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.97 KB | None | 0 0
  1. /* Write a program that reads some text from the console and counts the occurrences of each character in it. Print the results in alphabetical (lexicographical) order. */
  2.  
  3. namespace _06.CountSymbols
  4. {
  5.     using System;
  6.     using System.Collections.Generic;
  7.  
  8.     class CountSymbols
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Console.WriteLine("Enter your text on single line: ");
  13.             string text = Console.ReadLine();
  14.  
  15.             SortedDictionary<char, int> symbolCounter = new SortedDictionary<char, int>();
  16.             foreach (char symbol in text)
  17.             {
  18.                 if (!symbolCounter.ContainsKey(symbol))
  19.                 {
  20.                     symbolCounter[symbol] = 0;
  21.                 }
  22.                 symbolCounter[symbol]++;
  23.             }
  24.  
  25.             foreach (var pair in symbolCounter)
  26.             {
  27.                 Console.WriteLine("{0}: {1} time/s", pair.Key, pair.Value);
  28.             }
  29.         }
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement