mellowdeep

Encrypt the Messages

Nov 22nd, 2015
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.94 KB | None | 0 0
  1. using System;
  2.  
  3.  
  4. namespace EncryptTheMessages
  5. {
  6.     class EncryptTheMessages
  7.     {
  8.         static void Main()
  9.         {
  10.             int numberOfMsg = 0;
  11.             string result = "";
  12.  
  13.             string start = Console.ReadLine();
  14.             while (start != "START" && start != "start")
  15.             {
  16.                 start = Console.ReadLine();
  17.             }
  18.             string input = Console.ReadLine();
  19.  
  20.             while (input != "END" && input != "end")
  21.             {
  22.                 if (string.IsNullOrWhiteSpace(input))
  23.                 {
  24.                     input = Console.ReadLine();
  25.                     continue;
  26.                 }
  27.                 result += EncryptedWord(input);
  28.                 result += Environment.NewLine;
  29.                 input = Console.ReadLine();
  30.                 if (input == "End" && input == "end")
  31.                 {
  32.                     break;
  33.                 }
  34.                 numberOfMsg += Message(0);
  35.             }
  36.             if (numberOfMsg != 0)
  37.             {
  38.                 Console.WriteLine("Total number of messages: {0}",numberOfMsg);
  39.                 Console.WriteLine(result);
  40.             }
  41.             else
  42.             {
  43.                 Console.WriteLine("No messages sent.");
  44.             }
  45.  
  46.         }
  47.  
  48.         static private string EncryptedWord(string word)
  49.         {
  50.             string encryptedMsg = "";
  51.             int charDifference = 13;
  52.             for (int i = word.Length - 1; i >= 0; i--)
  53.             {
  54.                 if (char.IsLetter(word[i]))
  55.                 {
  56.                     if ((word[i] >= 'A' && word[i] <= 'M') || (word[i] >= 'a' && word[i] <= 'm'))
  57.                     {
  58.                         int swap = Convert.ToInt32(word[i]) + charDifference;
  59.                         encryptedMsg += Convert.ToChar(swap);
  60.                     }
  61.                     else if ((word[i] >= 'N' && word[i] <= 'Z') || (word[i] >= 'n' && word[i] <= 'z'))
  62.                     {
  63.                         int swap = Convert.ToInt32(word[i]) - charDifference;
  64.                         encryptedMsg += Convert.ToChar(swap);
  65.                     }
  66.                 }
  67.                 else
  68.                 {
  69.                     switch (word[i])
  70.                     {
  71.                         case ' ': encryptedMsg += '+'; break;
  72.                         case ',': encryptedMsg += '%'; break;
  73.                         case '.': encryptedMsg += '&'; break;
  74.                         case '?': encryptedMsg += '#'; break;
  75.                         case '!': encryptedMsg += '$'; break;
  76.                         default: encryptedMsg += word[i]; break;
  77.                     }
  78.  
  79.                 }
  80.             }
  81.             if (!(string.IsNullOrEmpty(encryptedMsg)))
  82.             {
  83.                  Message(0);
  84.             }
  85.             return encryptedMsg;
  86.         }
  87.  
  88.         static private int Message(int msg)
  89.         {
  90.             msg++;
  91.             return msg;
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment