Advertisement
Guest User

01. Password Reset

a guest
Apr 5th, 2020
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.93 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class PyrvaZadacha {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         String encrypted = scanner.nextLine();
  8.         String command = scanner.nextLine();
  9.  
  10.         while (!command.equals("Done")) {
  11.             String[] commandParts = command.split(" ");
  12.             String commandName = commandParts[0];
  13.             switch (commandName) {
  14.                 case "TakeOdd":
  15.                     StringBuilder newPassword = new StringBuilder();
  16.                     for (int i = 1; i < encrypted.length(); i += 2) {
  17.  
  18.                         newPassword.append(encrypted.charAt(i));
  19.                     }
  20.                     encrypted = newPassword.toString();
  21.                     System.out.println(encrypted);
  22.                     break;
  23.                 case "Cut":
  24.                     int startIndex = Integer.parseInt(commandParts[1]);
  25.                     int lenght = Integer.parseInt(commandParts[2]);
  26.                     String firstPart = encrypted.substring(0, startIndex);
  27.                     String secondPart = encrypted.substring(startIndex + lenght, encrypted.length());
  28.                     encrypted = firstPart + secondPart;
  29.                     System.out.println(encrypted);
  30.                     break;
  31.                 case "Substitute":
  32.                     String searchFor = commandParts[1];
  33.                     String replaceWith = commandParts[2];
  34.                     if(!encrypted.contains(searchFor)){
  35.                         System.out.println("Nothing to replace!");
  36.                     } else{
  37.                         encrypted = encrypted.replace(searchFor,replaceWith);
  38.                         System.out.println(encrypted);
  39.                         break;
  40.                     }}
  41.             command = scanner.nextLine();
  42.         }
  43.         System.out.println(String.format("Your password is: %s",encrypted));
  44.     }
  45.  
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement