Advertisement
dragonbs

Password Reset

Mar 27th, 2023
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.65 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. class Program
  5. {
  6.     static void Main()
  7.     {
  8.         string pass = Console.ReadLine();
  9.         string input;
  10.         while ((input = Console.ReadLine()) != "Done")
  11.         {
  12.             string[] cmd = input.Split();
  13.             switch (cmd[0])
  14.             {
  15.                 case "TakeOdd":
  16.                     pass = TakeOdd(pass);
  17.                     Console.WriteLine(pass);
  18.                     break;
  19.                 case "Cut":
  20.                     pass = Cut(pass, int.Parse(cmd[1]), int.Parse(cmd[2]));
  21.                     Console.WriteLine(pass);
  22.                     break;
  23.                 case "Substitute":
  24.                     pass = Substitute(pass, cmd[1], cmd[2]); break;
  25.             }
  26.         }
  27.         Console.WriteLine($"Your password is: {pass}");
  28.     }
  29.  
  30.     private static string Substitute(string pass, string subString, string replacement)
  31.     {
  32.         if (!pass.Contains(subString))
  33.         {
  34.             Console.WriteLine($"Nothing to replace!");
  35.             return pass;
  36.         }
  37.         string newPass = pass.Replace(subString, replacement);
  38.         Console.WriteLine(newPass);
  39.         return newPass;
  40.     }
  41.  
  42.     private static string Cut(string pass, int index, int length)
  43.     {
  44.         string subString = pass.Substring(index, length);
  45.         int position = pass.IndexOf(subString);
  46.         return pass.Remove(position, length);
  47.     }
  48.  
  49.     private static string TakeOdd(string pass)
  50.     {
  51.         StringBuilder newPass = new StringBuilder();
  52.         for (int i = 1; i < pass.Length; i += 2)
  53.             newPass.Append(pass[i]);
  54.         return newPass.ToString();
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement