Advertisement
svephoto

World Tour [C#]

Apr 2nd, 2021 (edited)
1,356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.79 KB | None | 0 0
  1. using System;
  2.  
  3. namespace WorldTour
  4. {
  5.     public class Program
  6.     {
  7.         public static void Main(string[] args)
  8.         {
  9.             string text = Console.ReadLine();
  10.            
  11.             string line = string.Empty;
  12.  
  13.             while (line != "Travel")
  14.             {
  15.                 line = Console.ReadLine();
  16.  
  17.                 string[] parts = line.Split(":", StringSplitOptions.RemoveEmptyEntries);
  18.  
  19.                 string command = parts[0];
  20.  
  21.                 if (command == "Add Stop")
  22.                 {
  23.                     int idx = int.Parse(parts[1]);
  24.                    
  25.                     if (idx >= 0 && idx < text.Length)
  26.                     {
  27.                         string substr = parts[2];
  28.                         text = text.Insert(idx, substr);
  29.                     }
  30.                    
  31.                     Console.WriteLine(text);                  
  32.                 }                              
  33.                 else if (command == "Remove Stop")
  34.                 {
  35.                     int startIdx = int.Parse(parts[1]);
  36.                     int endIdx = int.Parse(parts[2]);
  37.                    
  38.                     if (startIdx >= 0 && endIdx < text.Length)
  39.                     {
  40.                         text = text.Remove(startIdx, endIdx - startIdx + 1);
  41.                     }
  42.                    
  43.                     Console.WriteLine(text);                   
  44.                 }
  45.                 else if (command == "Switch")
  46.                 {
  47.                     string oldText = parts[1];
  48.                     string newText = parts[2];
  49.                    
  50.                     if (text.Contains(oldText))
  51.                     {
  52.                         text = text.Replace(oldText, newText);
  53.                     }
  54.                    
  55.                     Console.WriteLine(text);  
  56.                 }
  57.             }
  58.  
  59.             Console.WriteLine($"Ready for world tour! Planned stops: {text}");
  60.         }
  61.     }
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement