Advertisement
Guest User

Untitled

a guest
Jan 12th, 2021
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.83 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace World_Tour
  5. {
  6.     internal static class Program
  7.     {
  8.         private static void Main()
  9.         {
  10.             var route = Console.ReadLine();
  11.            
  12.             string command;
  13.             while ((command = Console.ReadLine()) != "Travel")
  14.             {
  15.                 var tokens = command.Split(":").ToArray();
  16.                
  17.                 route = tokens[0] switch
  18.                 {
  19.                     "Add Stop" => AddStop(route, int.Parse(tokens[1]), tokens[2]),
  20.                     "Remove Stop" => RemoveStop(route, int.Parse(tokens[1]), int.Parse(tokens[2])),
  21.                     "Switch" => ReplaceStops(route, tokens[1], tokens[2]),
  22.                     _ => route
  23.                 };
  24.  
  25.                 Console.WriteLine(route);
  26.             }
  27.  
  28.             Console.WriteLine($"Ready for world tour! Planned stops: {route}");
  29.         }
  30.  
  31.         private static string RemoveStop(string route, int startIndex, int endIndex)
  32.         {
  33.             if (IsIndexValid(route.Length, startIndex) && IsIndexValid(route.Length, endIndex))
  34.             {
  35.                 route = route.Remove(startIndex, endIndex - startIndex + 1);
  36.             }
  37.  
  38.             return route;
  39.         }
  40.  
  41.         private static string AddStop(string route, int startIndex, string newStop)
  42.         {
  43.             if (IsIndexValid(route.Length, startIndex))
  44.             {
  45.                 route = route.Insert(startIndex, newStop);
  46.             }
  47.  
  48.             return route;
  49.         }
  50.  
  51.         private static string ReplaceStops(string route, string oldStop, string newStop)
  52.         {
  53.             return route.Replace(oldStop, newStop);
  54.         }
  55.  
  56.         private static bool IsIndexValid(int length, int index)
  57.         {
  58.             return index >= 0 && index < length;
  59.         }
  60.     }
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement