Advertisement
silvi81

Encrypt the Messages

Nov 27th, 2015
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.65 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace EncryptTheMessages
  8. {
  9.     class EncryptTheMessages
  10.     {
  11.         static void Main()
  12.         {
  13.             StringBuilder encriptedMessage = new StringBuilder();
  14.             bool isEmpty = false;
  15.             bool isStart = false;
  16.             int count = 0;
  17.  
  18.             Dictionary<char, char> specialChar = new Dictionary<char, char>();
  19.             specialChar.Add(' ', '+');
  20.             specialChar.Add(',', '%');
  21.             specialChar.Add('.', '&');
  22.             specialChar.Add('?', '#');
  23.             specialChar.Add('!', '$');
  24.  
  25.             string command = Console.ReadLine();
  26.             while (true)
  27.             {
  28.                 if (command == "START" || command == "start")
  29.                 {
  30.                     isStart = true;
  31.                     command = Console.ReadLine();
  32.                 }
  33.                 if (command == "END" || command == "end")
  34.                 {
  35.                     isStart = false;
  36.                     break;
  37.                 }
  38.                 if (string.IsNullOrEmpty(command))
  39.                 {
  40.                     isEmpty = true;
  41.                 }
  42.                 if (isStart && isEmpty == false)
  43.                 {
  44.                     count++;
  45.                     command = specialChar.Aggregate(command, (result, s) => result.Replace(s.Key, s.Value));
  46.                    
  47.                     for (int i = command.Length - 1; i >=0 ; i--)
  48.                     {
  49.                         if ((command[i] >= 'A' && command[i] <= 'M') || (command[i] >= 'a' && command[i] <= 'm'))
  50.                         {
  51.                             encriptedMessage.Append((char)(command[i]+13));
  52.                         }
  53.                         else if ((command[i] >= 'N' && command[i] <= 'Z') || (command[i] >= 'n' && command[i] <= 'z'))
  54.                         {
  55.                             encriptedMessage.Append((char)(command[i] - 13));
  56.                         }
  57.                         else
  58.                            encriptedMessage.Append(command[i]);
  59.                     }
  60.                     encriptedMessage.AppendLine();
  61.                 }
  62.                 command = Console.ReadLine();
  63.                 isEmpty = false;
  64.             }
  65.  
  66.             if (count==0)
  67.             {
  68.                 Console.WriteLine("No messages sent.");
  69.             }
  70.             else
  71.             {
  72.                 Console.WriteLine("Total number of messages: {0}", count);
  73.                 encriptedMessage.ToString();
  74.                 Console.Write(encriptedMessage);
  75.             }
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement