Advertisement
gospod1978

Exam\Song Encryption

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