ellapt

T14.21.CountLetters

Feb 3rd, 2013
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. using System;
  2.  
  3. class CountLetters
  4. {
  5. static void Main()
  6. {
  7. Console.WriteLine("Print different letters in string with info how many times each letter is found\n");
  8.  
  9. string text = "The successful musical My Fair Lady was based on this Bernard Shaw classic.";
  10. Console.WriteLine("Original text:\n{0}\n", text);
  11.  
  12. int[] counts = new int['z' - 'a' + 1];
  13.  
  14. foreach (char ch in text.ToLower())
  15. {
  16. if ('a' <= ch && ch <= 'z') counts[ch - 'a']++;
  17. }
  18.  
  19. for (int i = 0; i < counts.Length; i++)
  20. {
  21. if (counts[i] != 0) Console.WriteLine("{0}: {1}", (char)(i + 'a'), counts[i]);
  22. }
  23. Console.WriteLine();
  24. }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment