fbinnzhivko

Decrypt the Messages

Mar 19th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.68 KB | None | 0 0
  1. using System;
  2.  
  3. public class EncryptTheMessages
  4. {
  5.     public static void Main()
  6.     {
  7.         string command = Console.ReadLine();
  8.  
  9.         while (command != "start" && command != "START")
  10.         {
  11.             command = Console.ReadLine();
  12.         }
  13.  
  14.         command = Console.ReadLine();
  15.  
  16.         int countOfMessages = 0;
  17.         string encryptedMessage = string.Empty;
  18.  
  19.         while (command != "end" && command != "END")
  20.         {
  21.             if (string.IsNullOrWhiteSpace(command))
  22.             {
  23.                 command = Console.ReadLine();
  24.                 continue;
  25.             }
  26.  
  27.             for (int i = command.Length - 1; i >= 0; i--)
  28.             {
  29.                 char currentSymbol = command[i];
  30.  
  31.                 if (('a' <= currentSymbol && currentSymbol <= 'm') || ('A' <= currentSymbol && currentSymbol <= 'M'))
  32.                 {
  33.                     encryptedMessage += (char)(currentSymbol + 13);
  34.                 }
  35.                 else if (('n' <= currentSymbol && currentSymbol <= 'z') || ('N' <= currentSymbol && currentSymbol <= 'Z'))
  36.                 {
  37.                     encryptedMessage += (char)(currentSymbol - 13);
  38.                 }
  39.                 else if ('0' <= currentSymbol && currentSymbol <= '9')
  40.                 {
  41.                     encryptedMessage += currentSymbol;
  42.                 }
  43.                 else
  44.                 {
  45.                     switch (currentSymbol)
  46.                     {
  47.                         case '+':
  48.                             encryptedMessage += ' ';
  49.                             break;
  50.                         case '%':
  51.                             encryptedMessage += ',';
  52.                             break;
  53.                         case '&':
  54.                             encryptedMessage += '.';
  55.                             break;
  56.                         case '#':
  57.                             encryptedMessage += '?';
  58.                             break;
  59.                         case '$':
  60.                             encryptedMessage += '!';
  61.                             break;
  62.                     }
  63.                 }
  64.             }
  65.  
  66.             /* add a new line symbol independent of the system (Linux, Windows)
  67.              * equivalent to "\n", but safer to use in different environments */
  68.             encryptedMessage += Environment.NewLine;
  69.             countOfMessages++;
  70.  
  71.             command = Console.ReadLine();
  72.         }
  73.  
  74.         if (countOfMessages == 0)
  75.         {
  76.             Console.WriteLine("No message received.");
  77.         }
  78.         else
  79.         {
  80.             Console.WriteLine("Total number of messages: {0}", countOfMessages);
  81.  
  82.             Console.Write(encryptedMessage);
  83.         }
  84.     }
  85. }
Add Comment
Please, Sign In to add comment