Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic; // needed for a List<T>
- using System.Text; // needed for a StringBuilder
- class EncryptTheMessages
- {
- static void Main()
- {
- string tempString = "";
- string inputString = "";
- int messageRows = -1; // message counter
- List<string> arrayMessage = new List<string>(); // container for input message strings
- StringBuilder encryptedString = new StringBuilder();
- int[] specialSymbolsCodes1 = { 32, 44, 46, 63, 33 }; // array for special symbols 1
- int[] specialSymbolsCodes2 = { 43, 37, 38, 35, 36 }; // array for special symbols 2
- do
- {
- tempString = Console.ReadLine(); // temp string - before the real message lines
- }
- while ((tempString != "start") && (tempString != "START"));
- do
- {
- inputString = Console.ReadLine(); // real string - with message
- if (inputString != "") // check for empty line
- {
- messageRows++;
- arrayMessage.Add(inputString);
- }
- }
- while ((inputString != "end") && (inputString != "END")); // entering "end" will stop entering messages
- arrayMessage.RemoveAt(messageRows); // remove the last iem in List: "end" word
- string[] stringArray = arrayMessage.ToArray(); //convert list to array with [messageRows] elements
- int asciiChar = 0; // ascii code for a current char
- for (int i = 0; i < stringArray.Length; i++) // loop for every line (message)
- {
- int lineLength = stringArray[i].Length; // length of the stringArray at position [i]
- for (int j = lineLength - 1; j >= 0; j -- ) // reverse loop for stringArray[i] --> from length-1 to 0
- {
- char currentChar = Convert.ToChar(stringArray[i].Substring(j, 1));
- asciiChar = currentChar; // get ascii code of currectchar
- // check ascii codes for a current char
- if (((asciiChar >= 65) && (asciiChar <= 77)) || ((asciiChar >= 97) && (asciiChar <= 109))) // letter [A...M]
- {
- asciiChar += 13;
- }
- else if (((asciiChar >= 78) && (asciiChar <= 90)) || ((asciiChar >= 110) && (asciiChar <= 122))) // [N...Z]
- {
- asciiChar -= 13;
- }
- else
- {
- for (int k = 0; k < specialSymbolsCodes1.Length; k ++) // loop check special characters
- {
- if (asciiChar == specialSymbolsCodes1[k])
- {
- asciiChar = specialSymbolsCodes2[k];
- }
- else if (asciiChar == specialSymbolsCodes2[k])
- {
- asciiChar = specialSymbolsCodes1[k];
- }
- }
- }
- char tempChar = (char)asciiChar;
- encryptedString = encryptedString.Append(tempChar);
- }
- encryptedString = encryptedString.Append("\n");
- }
- if (messageRows > 0)
- {
- Console.WriteLine("Total number of messages: {0}", messageRows);
- Console.WriteLine(encryptedString.ToString());
- encryptedString.Clear();
- }
- else
- {
- Console.WriteLine("No message sent.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement