Advertisement
desislava_topuzakova

1. The Imitation Game

Mar 23rd, 2023
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. package ExamPreparation1;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class FirstTask_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 input = scanner.nextLine();
  13.  
  14. while (!input.equals("Decode")){
  15.  
  16. if (input.contains("Move")) {
  17. //input = "Move|5".split("|") -> ["Move", "5"]
  18. int lettersCount = Integer.parseInt(input.split("\\|")[1]);
  19. //"owyouh" -> първите 5 букви "owyou" -> "howyou"
  20. String firstLetters = modifyMessage.substring(0, lettersCount); //буквите за местене ("owyou")
  21. modifyMessage.replace(0, lettersCount, ""); //заместваме с нищо
  22. //modifyMessage.delete(0, lettersCount); //изтриваме буквите
  23. modifyMessage.append(firstLetters);
  24. } else if (input.contains("Insert")) {
  25. //input = "Insert|2|abc".split("|") -> ["Insert", "2", "abc"]
  26. int index = Integer.parseInt(input.split("\\|")[1]); //индекс, на който ще вмъкваме
  27. String textToInsert = input.split("\\|")[2]; //текста, който трябва да вмъкнем
  28. modifyMessage.insert(index, textToInsert);
  29. } else if (input.contains("ChangeAll")) {
  30. //input = "ChangeAll|abc|wqe".split("|") -> ["ChangeAll", "abc", "wqa"]
  31. String textForChange = input.split("\\|")[1]; //текст, който трябва да се променя
  32. String replacement = input.split("\\|")[2]; //текст, който ще замества
  33.  
  34. String currentMessage = modifyMessage.toString(); //моментното съобщение
  35. currentMessage = currentMessage.replace(textForChange, replacement);
  36. modifyMessage = new StringBuilder(currentMessage);
  37. }
  38. input = scanner.nextLine();
  39. }
  40.  
  41. System.out.println("The decrypted message is: " + modifyMessage);
  42.  
  43.  
  44. }
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement