Advertisement
alexbancheva

The_Final_Quest_MidExam_10thOfMarch2019

Apr 13th, 2020
515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.61 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace The_Final_Quest_MidExam_10thOfMarch2019
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.            List<string> sentence = Console.ReadLine()
  12.                 .Split(" ")
  13.                 .ToList();
  14.  
  15.             string instruction = Console.ReadLine();
  16.  
  17.             while (instruction != "Stop")
  18.             {
  19.                 string[] splitedInstruction = instruction.Split(" ");
  20.                 string command = splitedInstruction[0];
  21.  
  22.                 if (command == "Delete")
  23.                 {
  24.                     int index =int.Parse(splitedInstruction[1]) + 1;
  25.  
  26.                     if (index > 0 && index <= sentence.Count)
  27.                     {
  28.                         sentence.RemoveAt(index);
  29.                     }
  30.  
  31.                 }
  32.                 else if (command == "Swap")
  33.                 {
  34.                     string firstWord = splitedInstruction[1];
  35.                     string secondWord = splitedInstruction[2];
  36.  
  37.                     int indexof1 = sentence.IndexOf(firstWord);
  38.                     int indexOf2 = sentence.IndexOf(secondWord);
  39.  
  40.                     sentence[indexof1] = secondWord;
  41.                     sentence[indexOf2] = firstWord;
  42.                 }
  43.                 else if (command == "Put")
  44.                 {
  45.                     string word = splitedInstruction[1];
  46.                     int index = int.Parse(splitedInstruction[2]) - 1;
  47.  
  48.                     if (index > 0 && index <= sentence.Count)
  49.                     {
  50.                         sentence.Insert(index, word);
  51.                     }
  52.                     else if (index == sentence.Count - 1)
  53.                     {
  54.                         sentence.Add(word);
  55.                     }
  56.  
  57.                 }
  58.                 else if (command == "Sort")
  59.                 {
  60.                     sentence.Sort();
  61.                     sentence.Reverse();
  62.                 }
  63.                 else if (command == "Replace")
  64.                 {
  65.                     string wordOne = splitedInstruction[1];
  66.                     string wordTwo = splitedInstruction[2];
  67.  
  68.                     int indexOf2 = sentence.IndexOf(wordTwo);
  69.  
  70.                     if (sentence.Contains(wordTwo))
  71.                     {
  72.                         sentence[indexOf2] = wordOne;
  73.                     }
  74.                 }
  75.  
  76.  
  77.                 instruction = Console.ReadLine();
  78.             }
  79.  
  80.             if (instruction == "Stop")
  81.             {
  82.                 Console.WriteLine(string.Join(" ", sentence));
  83.             }
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement