Advertisement
petar088

Untitled

Jul 23rd, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. package Fundamentals.Final_Exam.old_Exam;
  2.  
  3. import java.util.Scanner;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class Song_Encryption {
  8. public static void main(String[] args) {
  9. Scanner sc = new Scanner(System.in);
  10.  
  11. String regex = "^(?<artist>[A-Z][a-z ']+):(?<song>[A-Z ]+)$";
  12.  
  13. String input = sc.nextLine();
  14. while (!input.equals("end")) {
  15. Pattern pattern = Pattern.compile(regex);
  16. Matcher matcher = pattern.matcher(input);
  17. if (matcher.find()) {
  18. String artist = matcher.group("artist");
  19.  
  20. StringBuilder crypt = new StringBuilder();
  21.  
  22. for (int i = 0; i < input.length(); i++) {
  23. int key = artist.length();
  24.  
  25. if (Character.isUpperCase(input.charAt(i))) {
  26. if (input.charAt(i) + key > 90) {
  27. key = input.charAt(i) + key - 90;
  28. crypt.append(Character.toString(key + 64));
  29. } else {
  30. crypt.append(Character.toString(input.charAt(i) + key));
  31. }
  32.  
  33. } else if (Character.isLowerCase(input.charAt(i))) {
  34.  
  35. if (input.charAt(i) + key > 122) {
  36. key = input.charAt(i) + key - 122;
  37. crypt.append(Character.toString(key + 96));
  38. } else {
  39. crypt.append(Character.toString(input.charAt(i) + key));
  40. }
  41. } else if (input.charAt(i) == ':') {
  42. crypt.append("@");
  43. } else if (input.charAt(i) == ' ') {
  44. crypt.append(" ");
  45. }
  46. }
  47. System.out.println("Successful encryption: "+crypt);
  48. } else {
  49. System.out.println("Invalid input!");
  50. }
  51.  
  52. input = sc.nextLine();
  53. }
  54.  
  55.  
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement