Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package JavaFundamentals.FinalExamExamples;
- import javax.xml.stream.events.EndDocument;
- import java.util.Scanner;
- public class PasswordReset {
- public static void main(String[] args) {
- Scanner read = new Scanner(System.in);
- // all commands are :
- // --> TakeOdd
- // --> Cut {index} {length}
- // --> Substitute {substring} {substitute}
- // create a StringBuilder to hold the text and change it
- String password = read.nextLine();
- String command = read.nextLine();
- StringBuilder passwordToManipulate = new StringBuilder();
- // " Siiceercaroetavm!:?:ahsott.:i:nstupmomceqr "
- while (!command.equals("Done")) {
- String[] input = command.split("\\s+");
- String order = input[0];// --> taking commands from input !!
- switch (order) {
- case "TakeOdd": // taking digits only in Odd positions --> icecream::hot::summer
- for (int index = 1; index < password.length(); index += 2) {
- char current = password.charAt(index);
- passwordToManipulate.append(current);
- }
- password = passwordToManipulate.toString();
- System.out.println(password);
- break;
- case "Cut":
- int startIndex = Integer.parseInt(input[1]);
- int lengthToCut = Integer.parseInt(input[2]);
- String remove = password.substring(startIndex,startIndex + lengthToCut);
- password = password.replaceFirst(remove,"");
- System.out.println(password);
- break;
- case "Substitute":
- String toCheck = input[1];
- String toSubstitute = input[2];
- if (password.contains(toCheck)) {
- password = password.replace(toCheck, toSubstitute);
- System.out.println(password);
- } else {
- System.out.println("Nothing to replace!");
- }
- break;
- }
- command = read.nextLine();
- }
- System.out.printf("Your password is: %s",password);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement