Advertisement
Guest User

Untitled

a guest
Aug 17th, 2013
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.55 KB | None | 0 0
  1. /*Write a program that reads a string from the console and prints all different letters in the string along with information
  2.  how many times each letter is found. */
  3. using System;
  4. using System.Collections.Generic;
  5.  
  6. class LettersCount
  7. {
  8.     static void Main()
  9.     {
  10.         char[] specialSigns = { ' ', '?', '!', ';', ',', '\n', '\t', '\r', '.', '-', '_', '[', ']', '{', '}', '^', '&', '@', '#', '$', '%', '*', };
  11.         string text = "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();
  12.         string[] textWithoutSigns = text.Split(specialSigns);
  13.         int[] repeatingCounter = new int[26];
  14.         char[] allLeters = new char[26];
  15.  
  16.         int counter = 0;
  17.         for (char i = 'a'; i <= 'z'; i++)//I am getting all letters
  18.         {
  19.             allLeters[counter] = i;
  20.             counter++;
  21.         }
  22.  
  23.         foreach (var word in textWithoutSigns)
  24.         {
  25.             char[] wordAsChar = word.ToCharArray();
  26.             for (int i = 0; i < wordAsChar.Length; i++)
  27.             {
  28.                 for (int j = 0; j < 26; j++)
  29.                 {
  30.                     if (wordAsChar[i] == allLeters[j])
  31.                     {
  32.                         repeatingCounter[j]++;
  33.                         break;
  34.                     }
  35.                 }
  36.             }
  37.         }
  38.  
  39.         for (int i = 0; i < 26; i++)
  40.         {
  41.             Console.WriteLine("'" + allLeters[i] + "'" + " -> " + repeatingCounter[i]);
  42.         }
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement