Advertisement
LoraOrliGeo

Song Encryption

Apr 11th, 2019
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.03 KB | None | 0 0
  1. package finalExam_16Dec2018;
  2.  
  3. import java.util.Scanner;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class P2_SongEncryption {
  8.     public static void main(String[] args) {
  9.         @SuppressWarnings("resource")
  10.  
  11.         Scanner sc = new Scanner(System.in);
  12.  
  13.         String line = "";
  14.  
  15.         String regex = "^(?<artist>[A-Z][a-z'\\s]+):(?<song>[A-Z\\s]+)$";
  16.         Pattern pattern = Pattern.compile(regex);
  17.  
  18.         while (!"end".equals(line = sc.nextLine())) {
  19.             Matcher matcher = pattern.matcher(line);
  20.             String artist = "";
  21.             String song = "";
  22.             String encrypted = "";
  23.  
  24.             if (matcher.find()) {
  25.                 artist = matcher.group("artist");
  26.                 song = matcher.group("song");
  27.             } else {
  28.                 System.out.println("Invalid input!");
  29.                 continue;
  30.             }
  31.  
  32.             int key = artist.length();
  33.  
  34.             for (int i = 0; i < artist.length(); i++) {
  35.                 char symbol = artist.charAt(i);
  36.                 if (symbol == ' ' || symbol == '\'') {
  37.                     encrypted += symbol;
  38.                     continue;
  39.                 }
  40.  
  41.                 if (artist.charAt(i) + key > 'z') {
  42.                     int res = 'z' - artist.charAt(i);
  43.                     symbol = (char) ((key - res) + 'a' - 1);
  44.                     encrypted += symbol;
  45.                 } else {
  46.                     symbol = (char) (artist.charAt(i) + key);
  47.                     encrypted += symbol;
  48.                 }
  49.             }
  50.            
  51.             encrypted += '@';
  52.            
  53.             for (int i = 0; i < song.length(); i++) {
  54.                 char symbol = song.charAt(i);
  55.                 if (symbol == ' ' || symbol == '\'') {
  56.                     encrypted += symbol;
  57.                     continue;
  58.                 }
  59.  
  60.                 if (song.charAt(i) + key > 'Z') {
  61.                     int res = 'Z' - song.charAt(i);
  62.                     symbol = (char) ((key - res) + 'A' - 1);
  63.                     encrypted += symbol;
  64.                 } else {
  65.                     symbol = (char) (song.charAt(i) + key);
  66.                     encrypted += symbol;
  67.                 }
  68.             }
  69.            
  70.             System.out.println("Successful encryption: " + encrypted);
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement