Advertisement
azaria77

02. Song Encryption-Programming Fundamentals Final Exam Prep

Mar 31st, 2020
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Text.RegularExpressions;
  4.  
  5. namespace _02._Song_Encryption
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. string pattern = @"^([A-Z]{1}[a-z]+([ |']*[a-z]*)*):([A-Z]+( ?[A-Z]?)+)$";
  12. string input;
  13. while ((input = Console.ReadLine()) != "end")
  14. {
  15. Match match = Regex.Match(input, pattern);
  16. if (!match.Success)
  17. {
  18. Console.WriteLine("Invalid input!");
  19. }
  20. else
  21. {
  22. int key = match.Groups[1].Length;
  23.  
  24. string artist = match.Groups[1].Value;
  25. StringBuilder matches = new StringBuilder();
  26. matches.Append(match.Groups[1]);
  27. matches.Append('@');
  28. matches.Append(match.Groups[3]);
  29. StringBuilder encryptedArtistSong = new StringBuilder();
  30. foreach (char item in matches.ToString())
  31. {
  32.  
  33. if (!(item == ' ' || item == '\'' || item == '@'))
  34. {
  35. char newChar = ' ';
  36. if (char.IsUpper(item) && ((int)item + key) <= 90)
  37. {
  38. newChar = (char)((int)item + key);
  39.  
  40. }
  41. else if (char.IsUpper(item) && ((int)item + key) > 90)
  42. {
  43. newChar = (char)((int)item + key - 90 + 64);
  44. }
  45. else if (char.IsLower(item) && ((int)item + key) <= 122)
  46. {
  47. newChar = (char)((int)item + key);
  48.  
  49. }
  50. else if (char.IsLower(item) && ((int)item + key) > 122)
  51. {
  52. newChar = (char)((int)item + key - 122 + 96);
  53. }
  54. encryptedArtistSong.Append(newChar);
  55. }
  56. else
  57. {
  58. encryptedArtistSong.Append(item);
  59. }
  60.  
  61. }
  62. Console.WriteLine($"Successful encryption: {encryptedArtistSong}");
  63.  
  64. }
  65. }
  66. }
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement