Advertisement
Stephen_MS

Encrypt The Messages

Nov 25th, 2015
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;   // needed for a List<T>
  3. using System.Text;  // needed for a StringBuilder
  4.  
  5. class EncryptTheMessages
  6. {
  7.     static void Main()
  8.     {
  9.         string tempString = "";
  10.         string inputString = "";
  11.         int messageRows = -1;   // message counter
  12.         List<string> arrayMessage = new List<string>(); // container for input message strings
  13.         StringBuilder encryptedString = new StringBuilder();
  14.         int[] specialSymbolsCodes1 = { 32, 44, 46, 63, 33 };    // array for special symbols 1
  15.         int[] specialSymbolsCodes2 = { 43, 37, 38, 35, 36 };    // array for special symbols 2
  16.        
  17.         do
  18.         {
  19.             tempString = Console.ReadLine();    // temp string - before the real message lines
  20.         }
  21.         while ((tempString != "start") && (tempString != "START"));
  22.  
  23.         do
  24.         {
  25.             inputString = Console.ReadLine();   // real string - with message
  26.             if (inputString != "")      // check for empty line
  27.             {
  28.                 messageRows++;
  29.                 arrayMessage.Add(inputString);
  30.             }
  31.         }
  32.         while ((inputString != "end") && (inputString != "END"));   // entering "end" will stop entering messages
  33.         arrayMessage.RemoveAt(messageRows);     // remove the last iem in List: "end" word
  34.         string[] stringArray = arrayMessage.ToArray();  //convert list to array with [messageRows] elements
  35.         int asciiChar = 0;  // ascii code for a current char
  36.  
  37.         for (int i = 0; i < stringArray.Length; i++)    // loop for every line (message)
  38.         {
  39.             int lineLength = stringArray[i].Length;     // length of the stringArray at position [i]
  40.             for (int j = lineLength - 1; j >= 0; j -- ) // reverse loop for stringArray[i] --> from length-1 to 0
  41.             {
  42.                 char currentChar = Convert.ToChar(stringArray[i].Substring(j, 1));
  43.                 asciiChar = currentChar;        // get ascii code of currectchar
  44.  
  45.                 // check ascii codes for a current char
  46.                 if (((asciiChar >= 65) && (asciiChar <= 77)) || ((asciiChar >= 97) && (asciiChar <= 109))) // letter [A...M]
  47.                 {
  48.                     asciiChar += 13;
  49.                 }
  50.                 else if (((asciiChar >= 78) && (asciiChar <= 90)) || ((asciiChar >= 110) && (asciiChar <= 122)))  // [N...Z]
  51.                 {
  52.                     asciiChar -= 13;
  53.                 }
  54.                 else
  55.                 {
  56.                     for (int k = 0; k < specialSymbolsCodes1.Length; k ++)  // loop check special characters
  57.                     {
  58.                         if (asciiChar == specialSymbolsCodes1[k])
  59.                         {
  60.                             asciiChar = specialSymbolsCodes2[k];
  61.                         }
  62.                         else if (asciiChar == specialSymbolsCodes2[k])
  63.                         {
  64.                             asciiChar = specialSymbolsCodes1[k];
  65.                         }
  66.                     }
  67.                 }
  68.                 char tempChar = (char)asciiChar;
  69.                 encryptedString = encryptedString.Append(tempChar);
  70.             }
  71.             encryptedString = encryptedString.Append("\n");
  72.         }
  73.         if (messageRows > 0)
  74.         {
  75.             Console.WriteLine("Total number of messages: {0}", messageRows);
  76.             Console.WriteLine(encryptedString.ToString());
  77.             encryptedString.Clear();
  78.         }
  79.         else
  80.         {
  81.             Console.WriteLine("No message sent.");
  82.         }
  83.        
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement