Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package FinalExam;
- import java.util.Scanner;
- public class First {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String string = scanner.nextLine();
- String input = scanner.nextLine();
- while (!"Done".equals(input)) {
- String[] tokens = input.split("\\s+");
- String commander = tokens[0];
- switch (commander) {
- case "TakeOdd": {
- String temporary = "";
- for (int i = 1; i < string.length(); i += 2) {
- temporary += string.charAt(i);
- }
- string = temporary;
- System.out.println(string);
- break;
- }
- case "Cut": {
- int index = Integer.parseInt(tokens[1]);
- int length = Integer.parseInt(tokens[2]);
- String firstPart = string.substring(0, index);
- String secondPart = string.substring(index + length);
- string = firstPart + secondPart;
- System.out.println(string);
- break;
- }
- case "Substitute": {
- String searchFor = tokens[1];
- String replaceWith = tokens[2];
- String temporary = "";
- if (string.contains(String.valueOf(searchFor))) {
- temporary = string.replaceAll(searchFor, replaceWith);
- string = temporary;
- System.out.println(string);
- } else {
- System.out.println("Nothing to replace!");
- }
- break;
- }
- }
- input = scanner.nextLine();
- }
- System.out.println("Your password is:" + " " + string);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement