SHARE
TWEET

SongEncryption

a guest Dec 3rd, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5. public class SongEncryption {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         String line = scanner.nextLine();
  9.         String regex = "^([A-Z][a-z\' ]+):([A-Z\\\\s]+)$";
  10.         Pattern pattern = Pattern.compile(regex);
  11.  
  12.         while (!line.equals("end")) {
  13.             StringBuilder encrypted = new StringBuilder();
  14.             Matcher matcher = pattern.matcher(line);
  15.             if (matcher.find()){
  16.                 String artist = matcher.group(1);
  17.                 String song = matcher.group(2);
  18.  
  19.                 for (int i = 0; i <line.length() ; i++) {
  20.                   if (line.charAt(i) == ':'){
  21.                       encrypted.append("@");
  22.                   }else if (Character.isWhitespace(line.charAt(i))){
  23.                       encrypted.append(" ");
  24.                   }else if (line.charAt(i) == '\''){
  25.                       encrypted.append('\'');
  26.                   }else if (Character.isLetter(line.charAt(i))){
  27.                       int key = artist.length();
  28.                       char encryptedChar = (char)(line.charAt(i)+key);
  29.  
  30.                       if (Character.isLowerCase(encryptedChar) && encryptedChar > 'z'){
  31.                      encryptedChar = (char) ('a' + (encryptedChar - 'z' -1));
  32.                           encrypted.append((char)encryptedChar);
  33.                       }else if (Character.isUpperCase(encryptedChar) && encryptedChar > 90){
  34.                           encryptedChar =(char) ('A' + (encryptedChar - 'Z' -1));
  35.                           encrypted.append((char) encryptedChar);
  36.                       }else {
  37.  
  38.                           encrypted.append((char) encryptedChar);
  39.                       }
  40.                   }
  41.  
  42.                 }
  43.                 System.out.println("Successful encryption: "+ encrypted.toString());
  44.             }else {
  45.                 System.out.println("Invalid input!");
  46.             }
  47.  
  48.            
  49.  
  50.                     line = scanner.nextLine();
  51.         }
  52.     }
  53. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top