Advertisement
SotirovG

PasswordReset

Apr 1st, 2021
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.32 KB | None | 0 0
  1. package JavaFundamentals.FinalExamExamples;
  2.  
  3. import javax.xml.stream.events.EndDocument;
  4. import java.util.Scanner;
  5.  
  6. public class PasswordReset {
  7.     public static void main(String[] args) {
  8.         Scanner read = new Scanner(System.in);
  9.  
  10.         // all commands are :
  11.         // --> TakeOdd
  12.         // --> Cut {index} {length}
  13.         // --> Substitute {substring} {substitute}
  14.         // create a StringBuilder to hold the text and change it
  15.  
  16.         String password = read.nextLine();
  17.         String command = read.nextLine();
  18.         StringBuilder passwordToManipulate = new StringBuilder();
  19.         // " Siiceercaroetavm!:?:ahsott.:i:nstupmomceqr  "
  20.  
  21.         while (!command.equals("Done")) {
  22.  
  23.             String[] input = command.split("\\s+");
  24.             String order = input[0];// --> taking commands from input !!
  25.             switch (order) {
  26.                 case "TakeOdd": // taking digits only in Odd positions --> icecream::hot::summer
  27.                     for (int index = 1; index < password.length(); index += 2) {
  28.                         char current = password.charAt(index);
  29.                         passwordToManipulate.append(current);
  30.                     }
  31.                     password = passwordToManipulate.toString();
  32.                     System.out.println(password);
  33.  
  34.                     break;
  35.                 case "Cut":
  36.                     int startIndex = Integer.parseInt(input[1]);
  37.                     int lengthToCut = Integer.parseInt(input[2]);
  38.  
  39.                     String remove = password.substring(startIndex,startIndex + lengthToCut);
  40.                     password = password.replaceFirst(remove,"");
  41.  
  42.                     System.out.println(password);
  43.                     break;
  44.                 case "Substitute":
  45.                     String toCheck = input[1];
  46.                     String toSubstitute = input[2];
  47.                     if (password.contains(toCheck)) {
  48.  
  49.                        password =  password.replace(toCheck, toSubstitute);
  50.  
  51.                         System.out.println(password);
  52.                     } else {
  53.                         System.out.println("Nothing to replace!");
  54.                     }
  55.                     break;
  56.             }
  57.  
  58.             command = read.nextLine();
  59.         }
  60.         System.out.printf("Your password is: %s",password);
  61.  
  62.  
  63.     }
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement