Advertisement
ilianrusev

(DEMO)Final Exam - 06 April 2019 - 02. Deciphering

Apr 13th, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.45 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.*;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6. import java.util.stream.Collectors;
  7.  
  8. public class Main {
  9.  
  10.     public static void main(String[] args) {
  11.         Scanner scanner = new Scanner(System.in);
  12.  
  13.         String encrypted = scanner.nextLine();
  14.         String afterEncryption = "";
  15.         List<String> letters = Arrays
  16.                 .stream(scanner.nextLine()
  17.                 .split(" "))
  18.                 .collect(Collectors.toList());
  19.  
  20.         String regex = "^([d-z\\{\\}\\|#]+)";
  21.         Pattern pattern = Pattern.compile(regex);
  22.         Matcher matcher = pattern.matcher(encrypted);
  23.  
  24.         System.out.println();
  25.  
  26.         if (!matcher.find()){
  27.             System.out.println("This is not the book you are looking for.");
  28.         }else {
  29.             for (int i = 0; i < encrypted.length(); i++) {
  30.                 afterEncryption += (char)((int)encrypted.charAt(i) - 3);
  31.             }
  32.             String first = letters.get(0);
  33.             String second = letters.get(1);
  34.             if (afterEncryption.contains(first)) {
  35.                 int index = afterEncryption.indexOf(first);
  36.                 while (index != -1) {
  37.                     afterEncryption = afterEncryption.replace(first, second);
  38.                     index = afterEncryption.indexOf(first);
  39.                 }
  40.             }
  41.  
  42.  
  43.         }
  44.  
  45.         System.out.println(String.join(" ",afterEncryption));
  46.  
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement