Advertisement
dimipan80

Advanced Topics 11. Count of Letters

Jul 4th, 2014
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.59 KB | None | 0 0
  1. // Write a program that reads a list of letters and prints for each letter how many times it appears in the list. The letters should be listed in alphabetical order.
  2.  
  3. namespace _11.CountOfLetters
  4. {
  5.     using System;
  6.  
  7.     public class CountOfLetters
  8.     {
  9.         public static void Main(string[] args)
  10.         {
  11.             checked
  12.             {
  13.                 Console.WriteLine("Enter your List of letters on single line, separated by a space!");
  14.                 string inputLine = Console.ReadLine();
  15.  
  16.                 char[] separators = new char[] { ' ', ',', ';' };
  17.                 string[] letters = inputLine.Split(separators, StringSplitOptions.RemoveEmptyEntries);
  18.                 if (letters.Length > 0)
  19.                 {
  20.                     Array.Sort(letters);
  21.                     int countLetterAppears = 1;
  22.                     for (int i = 1; i < letters.Length; i++)
  23.                     {
  24.                         if (letters[i] == letters[i - 1])
  25.                         {
  26.                             countLetterAppears++;
  27.                         }
  28.                         else
  29.                         {
  30.                             Console.WriteLine("{0} -> {1}", letters[i - 1], countLetterAppears);
  31.                             countLetterAppears = 1;
  32.                         }
  33.                     }
  34.  
  35.                     Console.WriteLine("{0} -> {1}", letters[letters.Length - 1], countLetterAppears);
  36.                 }
  37.                 else
  38.                 {
  39.                     Console.WriteLine("Error! - Empty List!!!");
  40.                 }
  41.             }
  42.         }
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement