Advertisement
georgimanov

11. Count Letter

Apr 13th, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. /*
  2. Problem 11. Count of Letters
  3. 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. Use the input and output format from the examples below. Examples:
  4.  
  5. Input Output
  6. *
  7. b b a a b a -> 2
  8. b -> 3
  9. *
  10. h d h a a a s d f d a d j d s h a a a -> 6
  11. d -> 5
  12. f -> 1
  13. h -> 3
  14. j -> 1
  15. s -> 2
  16. */
  17.  
  18. using System;
  19. using System.Linq;
  20.  
  21. class CountLetter
  22. {
  23. static void Main(string[] args)
  24. {
  25. string input = Console.ReadLine();
  26.  
  27. char[] uniqueChars = input.Distinct().ToArray();
  28. char[] arr = input.ToArray();
  29.  
  30. Array.Sort(uniqueChars); //sorting also can be done with 2 for loops
  31. int counter = 0;
  32. for (int i = 1; i < uniqueChars.Length; i++) //start from 1 because 0 is ' ';
  33. {
  34. for (int j = 0; j < arr.Length; j++)
  35. {
  36. if (uniqueChars[i] == arr[j])
  37. {
  38. counter++;
  39. }
  40. }
  41. Console.WriteLine("{0} -> {1}", uniqueChars[i], counter);
  42. counter = 0;
  43. }
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement