Advertisement
NadezhdaGeorgieva

01. The Imitation Game

Dec 9th, 2020
646
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.65 KB | None | 0 0
  1. package bg.softuni.javafundamentals;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Fin03_20Aug_TheImitationGame {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         String message = scanner.nextLine();
  9.         String input = scanner.nextLine();
  10.         while (!"Decode".equals(input)){
  11.             String[] instructions = input.split("\\|");
  12.             switch (instructions[0]){
  13.                 case "Move":
  14.                     int numberOfLetters = Integer.parseInt(instructions[1]);
  15.                     String firstPart = message.substring(0, numberOfLetters);
  16.                     String secondPart = message.substring(numberOfLetters);
  17.                     message = secondPart + firstPart;
  18.                     break;
  19.                 case "Insert":
  20.                     int index = Integer.parseInt(instructions[1]);
  21.                     String value = instructions[2];
  22.                     StringBuilder sb = new StringBuilder();
  23.                     sb.append(message);
  24.                     sb.insert(index, value);
  25.                     message = sb.toString();
  26.                     break;
  27.                 case "ChangeAll":
  28.                     String substring = instructions[1];
  29.                     String replacement = instructions[2];
  30.                     message = message.replace(substring, replacement);
  31.                     break;
  32.                 default:
  33.                     System.out.println("Invalid command: " + instructions[0]);
  34.                     break;
  35.             }
  36.             input = scanner.nextLine();
  37.         }
  38.         System.out.println("The decrypted message is: " + message);
  39.     }
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement