Advertisement
Guest User

Untitled

a guest
Apr 8th, 2019
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.30 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Text.RegularExpressions;
  4.  
  5. namespace song_encryption
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string input = string.Empty;
  12.  
  13.             while ((input = Console.ReadLine()).ToLower() != "end")
  14.             {
  15.  
  16.                 string[] inputArray = input.Split(":");
  17.  
  18.                 string artistPattern = @"^[A-Z][a-z\s']+$";
  19.                 string songPattern = @"^[A-Z\s]+$";
  20.  
  21.                 string artist = inputArray[0];
  22.                 string song = inputArray[1];
  23.  
  24.                 var validArtist = Regex.Match(artist, artistPattern);
  25.                 var validSong = Regex.Match(song, songPattern);
  26.  
  27.                 if (validArtist.Success && validSong.Success)
  28.                 {
  29.                     StringBuilder encryptedArtist = new StringBuilder();
  30.                     StringBuilder encryptedSong = new StringBuilder();
  31.  
  32.                     int code = artist.Length;
  33.  
  34.                     for (int i = 0; i < artist.Length; i++)
  35.                     {
  36.                         int encryptedLetter = 0;
  37.                         if (char.IsUpper(artist[i]) && artist[i] + code > 90)
  38.                         {
  39.                             encryptedLetter = artist[i] + code - 26;
  40.                             encryptedArtist.Append((char)encryptedLetter);
  41.                         }
  42.                         else if(char.IsLower(artist[i]) && artist[i] + code > 122)
  43.                         {
  44.                             encryptedLetter = artist[i] + code - 26;
  45.                             encryptedArtist.Append((char)encryptedLetter);
  46.                         }
  47.                         else if(artist[i] == ' ')
  48.                         {
  49.                             encryptedArtist.Append(' ');
  50.                         }
  51.                         else if (artist[i] == '\'')
  52.                         {
  53.                             encryptedArtist.Append('\'');
  54.                         }
  55.                         else
  56.                         {
  57.                             encryptedArtist.Append((char)(artist[i] + code));
  58.                         }
  59.                     }
  60.  
  61.                     for (int i = 0; i < song.Length; i++)
  62.                     {
  63.                         int encryptedLetter = 0;
  64.                         if (song[i] + code > 90)
  65.                         {
  66.                             encryptedLetter = song[i] + code - 26;
  67.                             encryptedSong.Append((char)encryptedLetter);
  68.                         }
  69.                         else if (song[i] == ' ')
  70.                         {
  71.                             encryptedSong.Append(' ');
  72.                         }
  73.                         else if (song[i] == '\'')
  74.                         {
  75.                             encryptedSong.Append('\'');
  76.                         }
  77.                         else
  78.                         {
  79.                             encryptedSong.Append((char)(song[i] + code));
  80.                         }
  81.                     }
  82.  
  83.                     Console.WriteLine($"Successful encryption: {encryptedArtist}@{encryptedSong}");
  84.                 }
  85.  
  86.                 else
  87.                 {
  88.                     Console.WriteLine("Invalid input!");
  89.                 }
  90.             }
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement