lifesaver800

Secret Chat

Apr 24th, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class SecretChat {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         String string = scanner.nextLine();
  8.         String input = scanner.nextLine();
  9.  
  10.         while (!"Reveal".equals(input)) {
  11.             String[] tokens = input.split(":\\|:");
  12.             String command = tokens[0];
  13.  
  14.             switch (command) {
  15.                 case "InsertSpace":
  16.                     int index = Integer.parseInt(tokens[1]);
  17.                     String sub1 = string.substring(0, index);
  18.                     String sub2 = string.substring(index);
  19.                     string = sub1 + " " + sub2;
  20.                     System.out.println(string);
  21.                     break;
  22.                 case "Reverse":
  23.                     String substring = tokens[1];
  24.  
  25.                     if (string.contains(substring)) {
  26.                         StringBuilder sb = new StringBuilder();
  27.                         for (int i = 0; i < substring.length(); i++) {
  28.                             sb.append(substring.charAt(i));
  29.                         }
  30.                         sb.reverse();
  31.                         string = string.replaceFirst("[\\W][\\w]+", sb.toString());
  32.                         System.out.println(string);
  33.                     } else {
  34.                         System.out.println("error");
  35.                     }
  36.                     break;
  37.                 case "ChangeAll":
  38.                     string = string.replace(tokens[1], tokens[2]);
  39.                     System.out.println(string);
  40.                     break;
  41.             }
  42.  
  43.             input = scanner.nextLine();
  44.         }
  45.         System.out.println(String.format("You have a new text message: %s", string));
  46.     }
  47. }
Add Comment
Please, Sign In to add comment