gabi11

Stacks and Queues - 09. Simple Text Editor

May 11th, 2019
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.53 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace Advanced
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int number = int.Parse(Console.ReadLine());
  14.             string text = string.Empty;
  15.             var stackText = new Stack<string>();
  16.  
  17.             for (int i = 0; i < number; i++)
  18.             {
  19.                 string[] line = Console.ReadLine().Split();
  20.  
  21.                 string command = line[0];
  22.  
  23.                 switch (command)
  24.                 {
  25.                     case "1":
  26.                         string textToAdd = line[1];
  27.                         stackText.Push(text);
  28.                         text += textToAdd;
  29.                         break;
  30.  
  31.                     case "2":
  32.                         int count = int.Parse(line[1]);
  33.  
  34.                         if (count > text.Length)
  35.                         {
  36.                             count = Math.Min(count, text.Length);
  37.                         }
  38.  
  39.                         stackText.Push(text);
  40.                         text = text.Substring(0, text.Length - count);
  41.                         break;
  42.  
  43.                     case "3":
  44.                         int index = int.Parse(line[1]);
  45.                         Console.WriteLine(text[index - 1]);
  46.                         break;
  47.  
  48.                     case "4":
  49.                         text = stackText.Pop();
  50.                         break;
  51.                 }
  52.             }
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment