borovaneca

PasswordReset

Mar 28th, 2023
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 KB | None | 0 0
  1. package Exam;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7. public class PasswordReset {
  8.     public static void main(String[] args) throws IOException {
  9.         BufferedReader scan = new BufferedReader(new InputStreamReader(System.in));
  10.  
  11.         String text = scan.readLine();
  12.         StringBuilder sb = new StringBuilder();
  13.  
  14.         String command;
  15.         while (!"Done".equals(command = scan.readLine())) {
  16.             String[] tokens = command.split("\\s");
  17.             String currentCommand = tokens[0];
  18.             switch (currentCommand) {
  19.                 case "TakeOdd":
  20.                     text = takeOdd(text);
  21.                     break;
  22.  
  23.                 case "Cut":
  24.                     String substring = text.substring(Integer.parseInt(tokens[1]), Integer.parseInt(tokens[1]) + Integer.parseInt(tokens[2]));
  25.                     text = text.replaceFirst(substring, "");
  26.                     break;
  27.  
  28.                 case "Substitute":
  29.                     if (text.contains(tokens[1])) {
  30.                         text = text.replaceAll(tokens[1], tokens[2]);
  31.  
  32.                     } else {
  33.                         System.out.println("Nothing to replace!");
  34.                         continue;
  35.                     }
  36.                     break;
  37.             }
  38.             System.out.println(text);
  39.  
  40.         }
  41.         System.out.printf("Your password is: %s\n", text);
  42.  
  43.     }
  44.  
  45.     private static String takeOdd(String text) {
  46.         StringBuilder newText = new StringBuilder();
  47.         for (int i = 0; i < text.length(); i++) {
  48.             if (i % 2 != 0) {
  49.                 newText.append(String.valueOf(text.charAt(i)));
  50.             }
  51.         }
  52.         return newText.toString();
  53.     }
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment