Advertisement
Guest User

SongEncryption

a guest
Dec 17th, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.13 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4.  
  5. namespace P02
  6. {
  7.     class SongEncyrption
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string input = Console.ReadLine();
  12.  
  13.             while (input != "end")
  14.             {
  15.                 string[] currentSong = input.Split(':').ToArray();
  16.                 string artistName = currentSong[0];
  17.                 string songName = currentSong[1];
  18.  
  19.                 bool artistValid = ValidateArtist(artistName);
  20.                 bool songValid = ValidateSong(songName);
  21.  
  22.                 if (artistValid == false || songValid == false)
  23.                 {
  24.                     Console.WriteLine("Invalid input!");
  25.                     input = Console.ReadLine();
  26.                 }
  27.                 else
  28.                 {
  29.                     int encrCode = artistName.Length;
  30.                     string encryptedArtist = EncryptArtistName(artistName, encrCode);
  31.                     string encryptedSong = EncryptSongName(songName, encrCode);
  32.                     Console.WriteLine($"Successful encryption: {encryptedArtist}@{encryptedSong}");
  33.  
  34.                     input = Console.ReadLine();
  35.                 }
  36.             }
  37.         }
  38.  
  39.         public static string EncryptSongName(string songName, int encrCode)
  40.         {
  41.             StringBuilder song = new StringBuilder();
  42.  
  43.             for (int i = 0; i < songName.Length; i++)
  44.             {
  45.                 if (songName[i] != ' ' && songName[i] != '\'')
  46.                 {
  47.                     if (songName[i] + encrCode > 90)
  48.                     {
  49.                         song.Append((char)((songName[i] + encrCode) - 26));
  50.                     }
  51.                     else
  52.                     {
  53.                         song.Append((char)(songName[i] + encrCode));
  54.                     }
  55.                 }
  56.                 else
  57.                 {
  58.                     song.Append(songName[i]);
  59.                 }
  60.             }
  61.  
  62.             return song.ToString();
  63.         }
  64.  
  65.         public static string EncryptArtistName(string artistName, int encrCode)
  66.         {
  67.             StringBuilder artist = new StringBuilder();
  68.             for (int i = 0; i < artistName.Length; i++)
  69.             {
  70.                 if (artistName[i] != ' ' && artistName[i] != '\'')
  71.                 {
  72.                     if (char.IsUpper(artistName[i]) && artistName[i] + encrCode > 90)
  73.                     {
  74.                         artist.Append((char)((artistName[i] + encrCode) - 26));
  75.                     }
  76.                     else if (char.IsLower(artistName[i]) && artistName[i] + encrCode > 122)
  77.                     {
  78.                         artist.Append((char)((artistName[i] + encrCode) - 26));
  79.                     }
  80.                     else
  81.                     {
  82.                         artist.Append((char)(artistName[i] + encrCode));
  83.                     }
  84.                 }
  85.                 else
  86.                 {
  87.                     artist.Append(artistName[i]);
  88.                 }
  89.             }
  90.  
  91.             return artist.ToString();
  92.         }
  93.  
  94.         public static bool ValidateSong(string songName)
  95.         {
  96.             for (int i = 0; i < songName.Length; i++)
  97.             {
  98.                 if (char.IsUpper(songName[i]) || songName[i] == ' ')
  99.                 {
  100.                     continue;
  101.                 }
  102.                 else
  103.                 {
  104.                     return false;
  105.                 }
  106.             }
  107.  
  108.             return true;
  109.         }
  110.  
  111.         public static bool ValidateArtist(string artistName)
  112.         {
  113.             if (char.IsLower(artistName[0]))
  114.             {
  115.                 return false;
  116.             }
  117.             else
  118.             {
  119.                 for (int i = 1; i < artistName.Length; i++)
  120.                 {
  121.                     if (char.IsLower(artistName[i]) || artistName[i] == ' ' || artistName[i] == '\'')
  122.                     {
  123.                         continue;
  124.                     }
  125.                     else
  126.                     {
  127.                         return false;
  128.                     }
  129.                 }
  130.             }
  131.  
  132.             return true;
  133.         }
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement