Advertisement
Guest User

Untitled

a guest
May 18th, 2019
579
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Problem09SimpleTextEditor
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int numberOfOperations = int.Parse(Console.ReadLine());
  11.  
  12.  
  13.             string result = string.Empty;
  14.             Stack<string> stack = new Stack<string>();
  15.  
  16.             for (int i = 1; i <= numberOfOperations; i++)
  17.             {
  18.                 string[] currentCommand = Console.ReadLine()
  19.                     .Split(" ", StringSplitOptions.RemoveEmptyEntries);
  20.  
  21.                 if (currentCommand[0] == "1")
  22.                 {
  23.                     stack.Push(result);
  24.                     string stringToAppend = currentCommand[1];
  25.                     result += stringToAppend;
  26.                 }
  27.                 else if (currentCommand[0] == "2")
  28.                 {
  29.                     stack.Push(result);
  30.  
  31.                     int count = int.Parse(currentCommand[1]);
  32.                     if (result.Length >= count)
  33.                     {
  34.                         string substringToRemove = string.Empty;
  35.                         for (int j = result.Length - count; j < result.Length; j++)
  36.                         {
  37.                             substringToRemove += result[j];
  38.                         }
  39.                         result = result.Replace(substringToRemove, "");
  40.                     }
  41.                    
  42.                 }
  43.                 else if (currentCommand[0] == "3")
  44.                 {
  45.                     int index = int.Parse(currentCommand[1]);
  46.  
  47.                     Console.WriteLine(result[index - 1]);
  48.                 }
  49.                 else if (currentCommand[0] == "4")
  50.                 {
  51.                     if (stack.Count > 0)
  52.                     {
  53.                         result = stack.Pop();
  54.                     }
  55.                     else
  56.                     {
  57.                         result = string.Empty;
  58.                     }
  59.                 }
  60.             }
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement