Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.57 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4. namespace SongEncryption
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             string artistPattern = @"(^[A-Z ]['a-z ]*$)";
  11.             string songPattern = @"(^[A-Z ]*$)";
  12.  
  13.             Regex artistReg = new Regex(artistPattern);
  14.             Regex songReg = new Regex(songPattern);
  15.            
  16.             while (true)
  17.             {
  18.                 var tokens = Console.ReadLine().Split(":", StringSplitOptions.RemoveEmptyEntries);
  19.  
  20.                 string artist = tokens[0];
  21.  
  22.                 if (artist == "end")
  23.                 {
  24.                     break;
  25.                 }
  26.  
  27.                 string song = tokens[1];
  28.  
  29.                 Match artistMatch = artistReg.Match(artist);
  30.                 Match songMatch = songReg.Match(song);
  31.  
  32.                 if (artistMatch.Success && songMatch.Success)
  33.                 {
  34.                     int key = artist.Length;
  35.                     string newArtist = string.Empty;
  36.                     string newSong = string.Empty;
  37.  
  38.                     foreach (var symbol in artist)
  39.                     {
  40.                         if (char.IsWhiteSpace(symbol) || symbol == '\'')
  41.                         {
  42.                             newArtist += symbol;
  43.                             continue;
  44.                         }
  45.                         int newChar = symbol + key;
  46.  
  47.                         if (char.IsUpper(symbol))
  48.                         {
  49.                             if (newChar > 90)
  50.                             {
  51.                                 int temp = 0;
  52.                                 temp = newChar - 90 - 1;
  53.                                 newChar = 65 + temp;
  54.                             }
  55.                         }
  56.                         else
  57.                         {
  58.                             if (newChar > 122)
  59.                             {
  60.                                 int temp = 0;
  61.                                 temp = newChar - 122 - 1;
  62.                                 newChar = 97 + temp;
  63.                             }
  64.                         }
  65.                         newArtist += (char)newChar;
  66.                     }
  67.  
  68.                     foreach (var symbol in song)
  69.                     {
  70.                         if (char.IsWhiteSpace(symbol) || symbol == '\'')
  71.                         {
  72.                             newSong += symbol;
  73.                             continue;
  74.                         }
  75.                         int newChar = symbol + key;
  76.  
  77.                         if (char.IsUpper(symbol))
  78.                         {
  79.                             if (newChar > 90)
  80.                             {
  81.                                 int temp = 0;
  82.                                 temp = newChar - 90 - 1;
  83.                                 newChar = 65 + temp;
  84.                             }
  85.                         }
  86.                         else
  87.                         {
  88.                             if (newChar > 122)
  89.                             {
  90.                                 int temp = 0;
  91.                                 temp = newChar - 122 - 1;
  92.                                 newChar = 97 + temp;
  93.                             }
  94.                         }
  95.  
  96.                         newSong += (char)newChar;
  97.                     }
  98.  
  99.                     Console.WriteLine($"Successful encryption: {newArtist}@{newSong}");
  100.                 }
  101.                 else
  102.                 {
  103.                     Console.WriteLine("Invalid input!");
  104.                 }
  105.             }
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement