Advertisement
mivak

Problem12CountOfNames

Mar 31st, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. namespace Task12CountOfNames
  2. {
  3. using System;
  4. class Task12CountOfNames
  5. {
  6. static void Main()
  7. {
  8. //Write a program that reads a list of names and prints for each name how
  9. //many times it appears in the list. The names should be listed in alphabetical order.
  10. //Use the input and output format from the examples below.
  11.  
  12. Console.WriteLine("Please enter a sequence of names separated by space");
  13. string text = Console.ReadLine();
  14. int counter = 1;
  15. string[] names = text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  16.  
  17. Array.Sort(names);
  18.  
  19. for (int i = 0; i < names.Length - 1; i++)
  20. {
  21. if (i == names.Length - 2)
  22. {
  23. if (names[i] == names[i + 1])
  24. {
  25. counter++;
  26. Console.WriteLine(names[i] + " -> " + counter);
  27. }
  28. else
  29. {
  30. if (names[i] == names[i - 1])
  31. {
  32. Console.WriteLine(names[i] + " -> " + counter);
  33. counter = 1;
  34. }
  35. Console.WriteLine(names[i + 1] + " -> " + counter);
  36. counter = 1;
  37. }
  38. }
  39. else
  40. {
  41. if (names[i] == names[i + 1])
  42. {
  43. counter++;
  44. }
  45. else
  46. {
  47. Console.WriteLine(names[i] + " -> " + counter);
  48. counter = 1;
  49. }
  50. }
  51. }
  52.  
  53. }
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement