Advertisement
simonradev

ArrayMatcher

Mar 31st, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.10 KB | None | 0 0
  1. namespace ArrayMatcher
  2. {
  3.     using System;
  4.     using System.Text;
  5.  
  6.     public class ArrMatcher
  7.     {
  8.         public static StringBuilder result;
  9.  
  10.         public static void Main()
  11.         {
  12.             string[] arraysInfo = Console.ReadLine().Split('\\');
  13.  
  14.             string first = arraysInfo[0];
  15.             string second = arraysInfo[1];
  16.             string command = arraysInfo[2].ToLower();
  17.  
  18.             result = new StringBuilder();
  19.             switch (command)
  20.             {
  21.                 case "join":
  22.                     ExecuteTheCommand(first, second, false);
  23.                     break;
  24.  
  25.                 case "left exclude":
  26.                     ExecuteTheCommand(second, first, true);
  27.                     break;
  28.  
  29.                 case "right exclude":
  30.                     ExecuteTheCommand(first, second, true);
  31.                     break;
  32.                    
  33.                 default:
  34.                     break;
  35.             }
  36.  
  37.             SortTheResult();
  38.  
  39.             Console.WriteLine(result.ToString());
  40.         }
  41.  
  42.         private static void SortTheResult()
  43.         {
  44.             bool isSorted = false;
  45.             while (!isSorted)
  46.             {
  47.                 isSorted = true;
  48.  
  49.                 for (int currSymbol = 0; currSymbol < result.Length - 1; currSymbol++)
  50.                 {
  51.                     int asciiOfCurrSymbol = result[currSymbol];
  52.                     int asciiOfNextSymbol = result[currSymbol + 1];
  53.  
  54.                     if (asciiOfCurrSymbol > asciiOfNextSymbol)
  55.                     {
  56.                         char symbolHolder = result[currSymbol];
  57.                         result[currSymbol] = result[currSymbol + 1];
  58.                         result[currSymbol + 1] = symbolHolder;
  59.  
  60.                         isSorted = false;
  61.                     }
  62.                 }
  63.             }
  64.         }
  65.  
  66.         private static void ExecuteTheCommand(string toIterate, string toCheckForChars, bool exclude)
  67.         {
  68.             int conditionToMatch = exclude ? 0 : 1;
  69.  
  70.             foreach (char symbol in toIterate)
  71.             {
  72.                 int charIsContained = CheckIfCharIsContained(symbol, toCheckForChars);
  73.  
  74.                 if (conditionToMatch == charIsContained)
  75.                 {
  76.                     result.Append(symbol);
  77.                 }
  78.             }
  79.         }
  80.  
  81.         /// <summary>
  82.         /// Checks if a certain char is contained in a string
  83.         /// </summary>
  84.         /// <param name="toSearchFor">The char you want to check if it is in the string</param>
  85.         /// <param name="toCheckForChars">The string you want to check for the char</param>
  86.         /// <returns>0 if it is not contained and 1 if it is</returns>
  87.         private static int CheckIfCharIsContained(char toSearchFor, string toCheckForChars)
  88.         {
  89.             int toReturn = 0;
  90.  
  91.             foreach (char symbol in toCheckForChars)
  92.             {
  93.                 if (toSearchFor == symbol)
  94.                 {
  95.                     toReturn = 1;
  96.  
  97.                     break;
  98.                 }
  99.             }
  100.  
  101.             return toReturn;
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement