Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package finalExam_16Dec2018;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class P2_SongEncryption {
- public static void main(String[] args) {
- @SuppressWarnings("resource")
- Scanner sc = new Scanner(System.in);
- String line = "";
- String regex = "^(?<artist>[A-Z][a-z'\\s]+):(?<song>[A-Z\\s]+)$";
- Pattern pattern = Pattern.compile(regex);
- while (!"end".equals(line = sc.nextLine())) {
- Matcher matcher = pattern.matcher(line);
- String artist = "";
- String song = "";
- String encrypted = "";
- if (matcher.find()) {
- artist = matcher.group("artist");
- song = matcher.group("song");
- } else {
- System.out.println("Invalid input!");
- continue;
- }
- int key = artist.length();
- for (int i = 0; i < artist.length(); i++) {
- char symbol = artist.charAt(i);
- if (symbol == ' ' || symbol == '\'') {
- encrypted += symbol;
- continue;
- }
- if (artist.charAt(i) + key > 'z') {
- int res = 'z' - artist.charAt(i);
- symbol = (char) ((key - res) + 'a' - 1);
- encrypted += symbol;
- } else {
- symbol = (char) (artist.charAt(i) + key);
- encrypted += symbol;
- }
- }
- encrypted += '@';
- for (int i = 0; i < song.length(); i++) {
- char symbol = song.charAt(i);
- if (symbol == ' ' || symbol == '\'') {
- encrypted += symbol;
- continue;
- }
- if (song.charAt(i) + key > 'Z') {
- int res = 'Z' - song.charAt(i);
- symbol = (char) ((key - res) + 'A' - 1);
- encrypted += symbol;
- } else {
- symbol = (char) (song.charAt(i) + key);
- encrypted += symbol;
- }
- }
- System.out.println("Successful encryption: " + encrypted);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement