Advertisement
Lyubohd

Untitled

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