Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Exam;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class PasswordReset {
- public static void main(String[] args) throws IOException {
- BufferedReader scan = new BufferedReader(new InputStreamReader(System.in));
- String text = scan.readLine();
- StringBuilder sb = new StringBuilder();
- String command;
- while (!"Done".equals(command = scan.readLine())) {
- String[] tokens = command.split("\\s");
- String currentCommand = tokens[0];
- switch (currentCommand) {
- case "TakeOdd":
- text = takeOdd(text);
- break;
- case "Cut":
- String substring = text.substring(Integer.parseInt(tokens[1]), Integer.parseInt(tokens[1]) + Integer.parseInt(tokens[2]));
- text = text.replaceFirst(substring, "");
- break;
- case "Substitute":
- if (text.contains(tokens[1])) {
- text = text.replaceAll(tokens[1], tokens[2]);
- } else {
- System.out.println("Nothing to replace!");
- continue;
- }
- break;
- }
- System.out.println(text);
- }
- System.out.printf("Your password is: %s\n", text);
- }
- private static String takeOdd(String text) {
- StringBuilder newText = new StringBuilder();
- for (int i = 0; i < text.length(); i++) {
- if (i % 2 != 0) {
- newText.append(String.valueOf(text.charAt(i)));
- }
- }
- return newText.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment