Advertisement
dimipan80

Advanced Topics 9. Remove Names

Jul 3rd, 2014
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.60 KB | None | 0 0
  1. // Write a program that takes as input two lists of names and removes from the first list all names given in the second list. The input and output lists are given as words, separated by a space, each list at a separate line.
  2.  
  3. namespace _09.RemoveNames
  4. {
  5.     using System;
  6.     using System.Linq;
  7.  
  8.     public class RemoveNames
  9.     {
  10.         public static void Main(string[] args)
  11.         {
  12.             checked
  13.             {
  14.                 Console.WriteLine("Enter all names from the First List on single line, separated by a space!");
  15.                 string[] firstList = ReadNamesFromInputAndCreateStringArray();
  16.                 Console.WriteLine("Enter all names from the Second List on single line, separated by a space!");
  17.                 string[] secondList = ReadNamesFromInputAndCreateStringArray();
  18.                 for (int i = 0; i < firstList.Length; i++)
  19.                 {
  20.                     bool nameIsInBothLists = secondList.Contains(firstList[i]);
  21.                     if (!nameIsInBothLists)
  22.                     {
  23.                         Console.Write("{0} ", firstList[i]);
  24.                     }
  25.                 }
  26.  
  27.                 Console.WriteLine();
  28.             }
  29.         }
  30.  
  31.         private static string[] ReadNamesFromInputAndCreateStringArray()
  32.         {
  33.             checked
  34.             {
  35.                 string inputLine = Console.ReadLine();
  36.  
  37.                 char[] separators = new char[] { ' ', ',', ';' };
  38.                 string[] names = inputLine.Split(separators, StringSplitOptions.RemoveEmptyEntries);
  39.                 return names;
  40.             }
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement