Advertisement
MZlatev

Untitled

Dec 6th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. namespace _4._2_SongEncryption
  5. {
  6. class Program
  7. {
  8. static void Main()
  9. {
  10. string input = Console.ReadLine();
  11.  
  12. while (input != "end")
  13. {
  14. string artist = input.Substring(0, input.IndexOf(":"));
  15. string song = input.Substring(input.IndexOf(":") + 1);
  16.  
  17. if (ValidateArtist(artist) && ValidateSong(song))
  18. {
  19. int length = artist.Length;
  20.  
  21. StringBuilder sb = new StringBuilder();
  22.  
  23. for (int i = 0; i < input.Length; i++)
  24. {
  25. if (input[i] == ':')
  26. {
  27. sb.Append('@');
  28. }
  29.  
  30. else if (input[i] == ' ' || input[i] == '\'')
  31. {
  32. sb.Append(input[i]);
  33. }
  34.  
  35. else
  36. {
  37. char symbol = (char)(input[i] + length);
  38.  
  39. if (char.IsUpper(input[i]) && symbol > 'Z')
  40. {
  41. symbol = (char)(symbol - 26);
  42. }
  43.  
  44. else if (char.IsLower(input[i]) && symbol > 'z')
  45. {
  46. symbol = (char)(symbol - 26);
  47. }
  48.  
  49. sb.Append(symbol);
  50. }
  51. }
  52.  
  53. Console.WriteLine($"Successful encryption: {sb.ToString()}");
  54. }
  55.  
  56. else
  57. {
  58. Console.WriteLine("Invalid input!");
  59. }
  60.  
  61. input = Console.ReadLine();
  62. }
  63. }
  64.  
  65. public static bool ValidateSong(string song)
  66. {
  67. bool isValid = true;
  68.  
  69. for (int i = 0; i < song.Length; i++)
  70. {
  71. if (!char.IsUpper(song[i]) && song[i] != ' ')
  72. {
  73. isValid = false;
  74. break;
  75. }
  76. }
  77.  
  78. return isValid;
  79. }
  80.  
  81. public static bool ValidateArtist(string artist)
  82. {
  83. bool isValid = true;
  84.  
  85. if (!char.IsUpper(artist[0]))
  86. {
  87. isValid = false;
  88. }
  89.  
  90. for (int i = 1; i < artist.Length; i++)
  91. {
  92. if (!char.IsLower(artist[i]) && artist[i] != '\'' && artist[i] != ' ')
  93. {
  94. isValid = false;
  95. break;
  96. }
  97. }
  98.  
  99. return isValid;
  100. }
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement