Advertisement
mustafov

Array Matcher

Sep 3rd, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.82 KB | None | 0 0
  1. //Array Matcher
  2. //You are given a string that consists of two character arrays and a command. Your task is to create a new array from the given //two by executing the command.
  3. //If the command says "join" it means that you should create an array with elements that are contained in both arrays. If the //command says "right exclude" it means that the newly created array should contain only elements from the first array that are //not contained in the second array. If the command says "left exclude" it means that you should create an array with elements //from the second array that are not contained in the first array.
  4. //The newly created array should have its elements sorted by their ASCII code. Examples:
  5. //1.    You are given the array "ABCD", the array "CAFG" and the command "join". The new array should be "AC".
  6. //2.    You are given the array "ABCD", the array "CAFG" and the command "right exclude". The new array should be "BD".
  7. //3.    You are given the array "ABCD", the array "CAFG" and the command "left exclude". The new array should be "FG".
  8.  
  9.  
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15.  
  16. namespace ConsoleApplication4
  17. {
  18.     class Program
  19.     {
  20.         static void Main(string[] args)
  21.         {
  22.             string input = Console.ReadLine();
  23.             string[] text = input.Split('\\');
  24.             string firstInput = text[0];
  25.             string secondInput = text[1];
  26.             string thirdInput = text[2]; // join; right exclude; left exclude
  27.             int isEqual = 0;
  28.             StringBuilder arr = new StringBuilder();
  29.             int index = 0;
  30.             if (thirdInput == "join")
  31.             {
  32.                 for (int i = 0; i < firstInput.Length; i++)
  33.                 {
  34.                     for (int j = 0; j < secondInput.Length; j++)
  35.                     {
  36.                         if (firstInput[i] == secondInput[j])
  37.                         {
  38.                             arr.Insert(index, firstInput[i]);
  39.                             index++;
  40.                         }
  41.                     }
  42.                 }
  43.             }
  44.             else if (thirdInput == "right exclude")
  45.             {
  46.                 for (int i = 0; i < firstInput.Length; i++)
  47.                 {
  48.                     isEqual = 0;
  49.                     for (int j = 0; j < secondInput.Length; j++)
  50.                     {
  51.                         if (firstInput[i] == secondInput[j])
  52.                         {
  53.                             isEqual++;
  54.                         }
  55.                     }
  56.                     if (isEqual == 0)
  57.                     {
  58.                         arr.Insert(index,firstInput[i]);
  59.                         index++;
  60.                         //Console.Write(firstInput[i]);
  61.                     }
  62.                 }
  63.             }
  64.             else if (thirdInput == "left exclude")
  65.             {
  66.                 for (int i = 0; i < secondInput.Length; i++)
  67.                 {
  68.                     isEqual = 0;
  69.                     for (int j = 0; j < firstInput.Length; j++)
  70.                     {
  71.                         if (secondInput[i] == firstInput[j])
  72.                         {
  73.                             isEqual++;
  74.                         }
  75.                     }
  76.                     if (isEqual == 0)
  77.                     {
  78.                         arr.Insert(index, secondInput[i]);
  79.                         index++;
  80.                         //Console.Write(secondInput[i]);
  81.                     }
  82.                 }
  83.             }
  84.             char[] array = arr.ToString().ToCharArray();
  85.             Array.Sort<char>(array);
  86.             char [] arra = array.Distinct().ToArray();
  87.             for (int a = 0; a < arra.Length; a++ )
  88.             {
  89.                     Console.Write(arra[a]);
  90.             }
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement