desislava_topuzakova

01. Password Reset

Aug 6th, 2021
1,427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.96 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class PasswordReset_01 {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         String password = scanner.nextLine();
  7.         String command = scanner.nextLine();
  8.         //"TakeOdd" -> split(" ") -> ["TakeOdd"]
  9.         //"Cut {index} {length}" -> split(" ") -> ["Cut", "{index}", "{length}"]
  10.         //"Substitute {substring} {substitute}" -> split(" ") -> ["Substitute", "{substring}", "{substitute}"]
  11.         while(!command.equals("Done")) {
  12.             String [] commandParts = command.split("\\s+");
  13.             String commandName = commandParts[0];
  14.             //"Take Odd"
  15.             //"Cut"
  16.             //"Substitute"
  17.             switch(commandName) {
  18.                 case "TakeOdd":
  19.                     password = getOddIndexes(password);
  20.                     System.out.println(password);
  21.                     break;
  22.                 case "Cut":
  23.                     int index = Integer.parseInt(commandParts[1]); //15
  24.                     int length = Integer.parseInt(commandParts[2]); //3
  25.                     String substringToRemove = password.substring(index, index + length); //текстът, който трябва да премахна
  26.                     password = password.replaceFirst(substringToRemove, "");
  27.                     System.out.println(password);
  28.                     break;
  29.                 case "Substitute":
  30.                     String substring = commandParts[1];
  31.                     String substitute = commandParts[2];
  32.                     //проверка дали substring го има в паролата
  33.                     if(password.contains(substring)) {
  34.                         //заменям всички substring с substitute
  35.                         password = password.replaceAll(substring, substitute);
  36.                         System.out.println(password);
  37.                     } else {
  38.                         System.out.println("Nothing to replace!");
  39.                     }
  40.                     break;
  41.             }
  42.  
  43.             command = scanner.nextLine();
  44.         }
  45.  
  46.         System.out.printf("Your password is: %s", password);
  47.     }
  48.  
  49.     private static String getOddIndexes(String password) {
  50.         StringBuilder newPassword = new StringBuilder(); //построим нова парола
  51.         //всички символи на нечетни индекси
  52.         //1, 3 ,5, 7, 9....
  53.        /* Вариант 1:
  54.        for (int index = 0; index <= password.length() - 1; index++) {
  55.             if(index % 2 == 1) {
  56.                 char currentSymbol = password.charAt(index);
  57.                 newPassword.append(currentSymbol);
  58.             }
  59.         }*/
  60.         //Вариант 2:
  61.         for (int index = 1; index < password.length(); index += 2) {
  62.             char currentSymbol = password.charAt(index);
  63.             newPassword.append(currentSymbol);
  64.         }
  65.  
  66.         return newPassword.toString();
  67.     }
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment