Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class PasswordReset {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String password = scanner.nextLine();
- String input;
- while (!"Done".equals(input = scanner.nextLine())) {
- String[] commands = input.split(" ");
- switch (commands[0]) {
- case "TakeOdd":
- StringBuilder newPassword = new StringBuilder();
- for (int i = 1; i < password.length(); i += 2) {
- newPassword.append(password.charAt(i));
- }
- password = newPassword.toString();
- System.out.println(password);
- break;
- case "Cut":
- int startIndex = Integer.parseInt(commands[1]);
- int endIndex = startIndex + Integer.parseInt(commands[2]);
- password = new StringBuilder(password)
- .delete(startIndex, endIndex).toString();
- System.out.println(password);
- break;
- case "Substitute":
- String substitute = commands[1];
- String replacement = commands[2];
- if (password.contains(substitute)) {
- password = password.replace(substitute, replacement);
- System.out.println(password);
- } else {
- System.out.println("Nothing to replace!");
- }
- break;
- }
- }
- System.out.println("Your password is: " + password);
- }
- }
- /* String password = scanner.nextLine();
- String input = scanner.nextLine();
- while (!input.equals("Done")) {
- String[] substring = input.split("\\s+ ");
- String command = substring[0];
- switch (command) {
- case "TakeOdd":
- String rawPassword = "";
- for (int i = 1; i < password.length(); i += 2) {
- rawPassword += password.charAt(i);
- }
- password = rawPassword;
- System.out.println(password);
- break;
- case "Cut":
- int index = Integer.parseInt(substring[1]);
- int length = Integer.parseInt(substring[2]);
- String firstArg = password.substring(0, index);
- String secondArg = password.substring(index + length);
- password = firstArg + secondArg;
- System.out.println(password);
- break;
- case "Substitute":
- String substitute = substring[1];
- String replacement = substring[2];
- if (password.contains(String.valueOf(substitute))) {
- rawPassword = password.replaceAll(substitute, replacement);
- password = rawPassword;
- System.out.println(password);
- } else {
- System.out.println("Nothing to replace!");
- }
- break;
- }
- input = scanner.nextLine();
- }
- System.out.println("Your password is: " + " " + password);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment