Advertisement
pshipkovenski

CountLetters

Jul 2nd, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class CountLetters
  5. {
  6.  
  7.     private static bool CharExistIn(List<char> lettersList, char currentChar, ref int letterIndex)
  8.     {
  9.         bool hasFound = false;
  10.  
  11.         for (int i = 0; i < lettersList.Count; i++)
  12.         {
  13.             if (currentChar == lettersList[i])
  14.             {
  15.                 letterIndex = i;
  16.                 hasFound = true;
  17.                 break;
  18.             }
  19.         }
  20.         return hasFound;
  21.     }
  22.  
  23.     static void Main()
  24.     {
  25.         Console.WriteLine("input string:");
  26.         string inputStr = Console.ReadLine();
  27.  
  28.         List<char> lettersList = new List<char>();
  29.         List<int> occurencesList = new List<int>();
  30.         int letterIndex = -1;
  31.  
  32.         for (int i = 0; i < inputStr.Length; i++)
  33.         {
  34.             char currentChar = inputStr[i];
  35.  
  36.             if (char.IsLetter(currentChar))
  37.             {
  38.                 if (CharExistIn(lettersList, currentChar, ref letterIndex))
  39.                 {
  40.                     occurencesList[letterIndex]++;
  41.                 }
  42.                 else
  43.                 {
  44.                     lettersList.Add(currentChar);
  45.                     occurencesList.Add(1);
  46.                 }
  47.             }
  48.         }
  49.  
  50.         for (int i = 0; i < lettersList.Count; i++)
  51.         {
  52.             Console.WriteLine("{0} --> {1}", lettersList[i], occurencesList[i]);
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement