Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package String;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- String command = scanner.nextLine();
- while (!"Reveal".equals(command)) {
- String[] tokens = command.split(":\\|:");
- String currentCommand = tokens[0];
- StringBuilder stringBuilder = new StringBuilder();
- switch (currentCommand) {
- case "InsertSpace":
- stringBuilder.append(input);
- int index = Integer.parseInt(tokens[1]);
- stringBuilder.insert(index, " ");
- input = String.valueOf(stringBuilder);
- System.out.println(input);
- break;
- case "Reverse":
- String substring = tokens[1];
- if (input.contains(substring)) {
- int startIndex = input.indexOf(tokens[1]);
- int endIndex = startIndex + substring.length();
- stringBuilder.append(substring);
- stringBuilder.reverse();
- input = input.substring(0, startIndex) + input.substring(endIndex) + stringBuilder;
- System.out.println(input);
- } else {
- System.out.println("error");
- }
- break;
- case "ChangeAll":
- input = input.replaceAll(tokens[1], tokens[2]);
- System.out.println(input);
- break;
- }
- command = scanner.nextLine();
- }
- System.out.printf("You have a new text message: %s", input);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement