Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class SecretChat {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String string = scanner.nextLine();
- String input = scanner.nextLine();
- while (!"Reveal".equals(input)) {
- String[] tokens = input.split(":\\|:");
- String command = tokens[0];
- switch (command) {
- case "InsertSpace":
- int index = Integer.parseInt(tokens[1]);
- String sub1 = string.substring(0, index);
- String sub2 = string.substring(index);
- string = sub1 + " " + sub2;
- System.out.println(string);
- break;
- case "Reverse":
- String substring = tokens[1];
- if (string.contains(substring)) {
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < substring.length(); i++) {
- sb.append(substring.charAt(i));
- }
- sb.reverse();
- string = string.replaceFirst("[\\W][\\w]+", sb.toString());
- System.out.println(string);
- } else {
- System.out.println("error");
- }
- break;
- case "ChangeAll":
- string = string.replace(tokens[1], tokens[2]);
- System.out.println(string);
- break;
- }
- input = scanner.nextLine();
- }
- System.out.println(String.format("You have a new text message: %s", string));
- }
- }
Add Comment
Please, Sign In to add comment