Advertisement
Guest User

05. String Commander

a guest
Nov 12th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp231
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             var text = Console.ReadLine();
  14.             while (true)
  15.             {
  16.                 var command = Console.ReadLine().Split();
  17.                 if (command[0] == "end")
  18.                 {
  19.                     break;
  20.                 }
  21.                 if (command[0] == "Left")
  22.                 {
  23.                     var count = int.Parse(command[1]);
  24.                     for (int i = 0; i < count; i++)
  25.                     {
  26.                         var firstSymbol = text[0];
  27.                         text = text.Remove(0, 1);
  28.                         text += firstSymbol;
  29.                     }                  
  30.                 }
  31.                 if (command[0] == "Right")
  32.                 {
  33.                     var count = int.Parse(command[1]);
  34.                     for (int i = 0; i < count; i++)
  35.                     {
  36.                         var lastSymbol = text[text.Length-1];
  37.                         text = text.Remove(text.Length - 1, 1);
  38.                         var finalresult = (lastSymbol + text);
  39.                         text = finalresult;
  40.                     }
  41.                 }
  42.                 if (command[0] == "Insert")
  43.                 {
  44.                     var index = int.Parse(command[1]);
  45.                     text = text.Insert(index, command[2]);
  46.                 }
  47.                 if (command[0] == "Delete")
  48.                 {
  49.                     var startIndex = int.Parse(command[1]);
  50.                     var endIndex = int.Parse(command[2]);
  51.                     var len = text.Length - startIndex;
  52.                     if(startIndex>=0 && endIndex <= len)
  53.                     {
  54.                         text = text.Remove(startIndex, endIndex + 1);
  55.                     }
  56.                     else
  57.                     {
  58.                         text = text.Remove(startIndex, len);
  59.                     }
  60.                    
  61.                 }
  62.  
  63.             }
  64.             Console.WriteLine(text);
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement