Advertisement
desislava_topuzakova

1. The Imitation Game

Jul 22nd, 2023
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. package exam_prepration;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Task_01 {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8.  
  9. String encryptedMessage = scanner.nextLine(); //криптирано съобщение
  10. StringBuilder modifyMessage = new StringBuilder(encryptedMessage); //модифицирам криптираното съобщение
  11.  
  12. String command = scanner.nextLine();
  13. while(!command.equals("Decode")) {
  14. if (command.contains("Move")) {
  15. //command = "Move|5".split("|") -> ["Move", "5"]
  16. int countLetters = Integer.parseInt(command.split("\\|")[1]);//брой на буквите, които трябва да взема от текста
  17. //"Desislava" -> първите 5 -> "Desis"
  18. String firstLetters = modifyMessage.substring(0, countLetters);
  19. //премахвам от текста
  20. modifyMessage.delete(0, countLetters); //премахвам взетите букви от началото -> "lava"
  21. //modifyMessage.replace(0, countLetters, "");
  22. //слагам накрая -> "lava" + "Desis" = "lavaDesis"
  23. modifyMessage.append(firstLetters);
  24. } else if (command.contains("Insert")) {
  25. //command = "Insert|2|Desi".split("|") -> ["Insert", "2", "Desi"]
  26. int index = Integer.parseInt(command.split("\\|")[1]); //индекс, на който искаме да вмъкнем
  27. String textToInsert = command.split("\\|")[2]; //текст за вмъкване
  28. modifyMessage.insert(index, textToInsert);
  29. } else if (command.contains("ChangeAll")) {
  30. //command = "ChangeAll|ab|vr".split("|") -> ["ChangeAll", "ab", "vr"]
  31. String textForChange = command.split("\\|")[1]; //текст, който трябва да се замени
  32. String replacement = command.split("\\|")[2]; //текст, който ще замества
  33.  
  34. String currentMessage = modifyMessage.toString(); //моментно съобщение
  35. currentMessage = currentMessage.replace(textForChange, replacement); //замяна в текста
  36. modifyMessage = new StringBuilder(currentMessage);
  37. }
  38.  
  39. command = scanner.nextLine();
  40. }
  41.  
  42. System.out.println("The decrypted message is: " + modifyMessage);
  43.  
  44.  
  45. }
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement