Didart

Password Reset

Nov 16th, 2021
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.46 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class PasswordReset {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         String password = scanner.nextLine();
  8.         String input;
  9.  
  10.         while (!"Done".equals(input = scanner.nextLine())) {
  11.  
  12.             String[] commands = input.split(" ");
  13.  
  14.             switch (commands[0]) {
  15.                 case "TakeOdd":
  16.                     StringBuilder newPassword = new StringBuilder();
  17.  
  18.                     for (int i = 1; i < password.length(); i += 2) {
  19.                         newPassword.append(password.charAt(i));
  20.                     }
  21.                     password = newPassword.toString();
  22.                     System.out.println(password);
  23.                     break;
  24.                 case "Cut":
  25.                     int startIndex = Integer.parseInt(commands[1]);
  26.                     int endIndex = startIndex + Integer.parseInt(commands[2]);
  27.                     password = new StringBuilder(password)
  28.                             .delete(startIndex, endIndex).toString();
  29.                     System.out.println(password);
  30.                     break;
  31.                 case "Substitute":
  32.                     String substitute = commands[1];
  33.                     String replacement = commands[2];
  34.  
  35.                     if (password.contains(substitute)) {
  36.                         password = password.replace(substitute, replacement);
  37.                         System.out.println(password);
  38.                     } else {
  39.                         System.out.println("Nothing to replace!");
  40.                     }
  41.                     break;
  42.             }
  43.         }
  44.         System.out.println("Your password is: " + password);
  45.     }
  46.  
  47. }
  48.  
  49.  
  50. /*        String password = scanner.nextLine();
  51.         String input = scanner.nextLine();
  52.  
  53.         while (!input.equals("Done")) {
  54.             String[] substring = input.split("\\s+ ");
  55.             String command = substring[0];
  56.  
  57.             switch (command) {
  58.                 case "TakeOdd":
  59.                     String rawPassword = "";
  60.                     for (int i = 1; i < password.length(); i += 2) {
  61.                         rawPassword += password.charAt(i);
  62.                     }
  63.                     password = rawPassword;
  64.  
  65.                     System.out.println(password);
  66.                     break;
  67.                 case "Cut":
  68.                     int index = Integer.parseInt(substring[1]);
  69.                     int length = Integer.parseInt(substring[2]);
  70.  
  71.                     String firstArg = password.substring(0, index);
  72.                     String secondArg = password.substring(index + length);
  73.                     password = firstArg + secondArg;
  74.  
  75.                     System.out.println(password);
  76.  
  77.                     break;
  78.                 case "Substitute":
  79.                     String substitute = substring[1];
  80.                     String replacement = substring[2];
  81.  
  82.                     if (password.contains(String.valueOf(substitute))) {
  83.                         rawPassword = password.replaceAll(substitute, replacement);
  84.                         password = rawPassword;
  85.                         System.out.println(password);
  86.                     } else {
  87.                         System.out.println("Nothing to replace!");
  88.                     }
  89.                     break;
  90.             }
  91.             input = scanner.nextLine();
  92.         }
  93.         System.out.println("Your password is: " + " " + password);
  94.     }
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment