Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Array Matcher
- //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.
- //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.
- //The newly created array should have its elements sorted by their ASCII code. Examples:
- //1. You are given the array "ABCD", the array "CAFG" and the command "join". The new array should be "AC".
- //2. You are given the array "ABCD", the array "CAFG" and the command "right exclude". The new array should be "BD".
- //3. You are given the array "ABCD", the array "CAFG" and the command "left exclude". The new array should be "FG".
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApplication4
- {
- class Program
- {
- static void Main(string[] args)
- {
- string input = Console.ReadLine();
- string[] text = input.Split('\\');
- string firstInput = text[0];
- string secondInput = text[1];
- string thirdInput = text[2]; // join; right exclude; left exclude
- int isEqual = 0;
- StringBuilder arr = new StringBuilder();
- int index = 0;
- if (thirdInput == "join")
- {
- for (int i = 0; i < firstInput.Length; i++)
- {
- for (int j = 0; j < secondInput.Length; j++)
- {
- if (firstInput[i] == secondInput[j])
- {
- arr.Insert(index, firstInput[i]);
- index++;
- }
- }
- }
- }
- else if (thirdInput == "right exclude")
- {
- for (int i = 0; i < firstInput.Length; i++)
- {
- isEqual = 0;
- for (int j = 0; j < secondInput.Length; j++)
- {
- if (firstInput[i] == secondInput[j])
- {
- isEqual++;
- }
- }
- if (isEqual == 0)
- {
- arr.Insert(index,firstInput[i]);
- index++;
- //Console.Write(firstInput[i]);
- }
- }
- }
- else if (thirdInput == "left exclude")
- {
- for (int i = 0; i < secondInput.Length; i++)
- {
- isEqual = 0;
- for (int j = 0; j < firstInput.Length; j++)
- {
- if (secondInput[i] == firstInput[j])
- {
- isEqual++;
- }
- }
- if (isEqual == 0)
- {
- arr.Insert(index, secondInput[i]);
- index++;
- //Console.Write(secondInput[i]);
- }
- }
- }
- char[] array = arr.ToString().ToCharArray();
- Array.Sort<char>(array);
- char [] arra = array.Distinct().ToArray();
- for (int a = 0; a < arra.Length; a++ )
- {
- Console.Write(arra[a]);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement