Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Write a program that reads a list of names and prints for each name how many times it appears in the list. The names should be listed in alphabetical order.
- namespace _12.CountOfNames
- {
- using System;
- public class CountOfNames
- {
- public static void Main(string[] args)
- {
- checked
- {
- Console.WriteLine("Enter your List of names on single line, separated by a space!");
- string inputLine = Console.ReadLine();
- char[] separators = new char[] { ' ', ',', ';' };
- string[] names = inputLine.Split(separators, StringSplitOptions.RemoveEmptyEntries);
- if (names.Length > 0)
- {
- Array.Sort(names);
- int countNameAppears = 1;
- for (int i = 1; i < names.Length; i++)
- {
- if (names[i] == names[i - 1])
- {
- countNameAppears++;
- }
- else
- {
- Console.WriteLine("{0} -> {1}", names[i - 1], countNameAppears);
- countNameAppears = 1;
- }
- }
- Console.WriteLine("{0} -> {1}", names[names.Length - 1], countNameAppears);
- }
- else
- {
- Console.WriteLine("Error! - Empty List!!!");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement