Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Decrypt the Messages
- //You are working for a company which is very concerned about its information and communication. For this reason, they have //invented an internal approach to communication between different departments – they are communicating to each other via messages, //which are reversed (written backwards) and then encrypted. In order to be read and understood, each message has to be decrypted. //Your task is to write a program, which receives all encrypted messages in a specific communication, decrypts them and prints all //decrypted messages at the console as well as the total number of messages that have been received.
- //At the beginning of a communication, you will receive either the keyword “START” (upper case) or “start” (lower case), which //indicates that you will start receiving reversed and encrypted messages. At the end of the communication, you will receive either //the keyword “END” (upper case) or “end” (lower case), which indicates that the communication is over and you need to show the //decrypted messages’ content and total count. Any non-empty string between the “start” and “end” keywords is considered a message. //If no messages have been received between the “start” and the “end” keywords, you should print on the console: “No message //received.”
- //All messages are case-sensitive and consist of letters, digits, as well as some special characters – ‘+’, ‘%’, ‘&’, ‘#’ and ‘$’. //Letters from A to M are converted to letters from N to Z (A N; B O; … M Z) and letters from N to Z are converted to letters //from A to M (N A; O B; … Z M). The converted letter should keep the case of the original letter. The special characters are //converted in the following way: ‘+’ is converted to a single space (‘ ’), ‘%’ is converted to a comma (‘,’), ‘&’ is converted to //a dot (‘.’), ‘#’ is converted to a question mark (‘?’) and ‘$’ is converted to an exclamation mark (‘!’). The digits (0-9) are //not converted and stay the same.
- //For example, you receive the following message – “$1+rtnffrz+greprF” and you start decrypting it. Convert the 1st character ‘$’ //to ‘!’, then the 2nd character – ‘1’ stays the same, then covert the 3rd character – ‘+’ to single space ‘ ’, ‘r’ ’e’, ‘t’ //‘g’, ‘n’ ‘a’, ‘f’ ‘s’, ‘f’ ‘s’, ‘r’ ’e’ , ‘z’ ’m’, ‘+’ ‘ ’, ‘g’ ‘t’, ‘r’ ’e’ , ‘e’ ’r’ , ‘p’ ’c’ , ‘r’ ’e’ //, ‘F’ ’S’. After decrypting all letters, the message is: “!1 egassem terceS” and when you reverse it, you get the original //message: “Secret message 1!”
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApplication2
- {
- class Program
- {
- static void Main(string[] args)
- {
- string input;
- StringBuilder newInput = new StringBuilder();
- int p = 0;
- List<string> arr = new List<string>();
- int messages = 0;
- do
- {
- input = Console.ReadLine();
- if (input == "START" || input == "start")
- {
- int lenght = input.Length;
- p = 0;
- newInput = new StringBuilder();
- if (input != "end" && input != "END" && input != "start" && input != "START")
- {
- for (int i = 0; i < lenght; i++)
- {
- if (input[i] >= 'a' && input[i] <= 'm')
- {
- newInput.Insert(p, (char)(input[i] + 13));
- p++;
- }
- else if (input[i] >= 'n' && input[i] <= 'z')
- {
- newInput.Insert(p, (char)(input[i] - 13));
- p++;
- }
- else if (input[i] >= 'A' && input[i] <= 'M')
- {
- newInput.Insert(p, (char)(input[i] + 13));
- p++;
- }
- else if (input[i] >= 'N' && input[i] <= 'Z')
- {
- newInput.Insert(p, (char)(input[i] - 13));
- p++;
- }
- else if (input[i] == '+')
- {
- newInput.Insert(p, (char)(input[i] - 11));
- p++;
- }
- else if (input[i] == '%')
- {
- newInput.Insert(p, (char)(input[i] + 7));
- p++;
- }
- else if (input[i] == '#')
- {
- newInput.Insert(p, (char)(input[i] + 28));
- p++;
- }
- else if (input[i] == '$')
- {
- newInput.Insert(p, (char)(input[i] - 3));
- p++;
- }
- else if (input[i] == '&')
- {
- newInput.Insert(p, (char)(input[i] + 8));
- p++;
- }
- else if (input[i] >= '0' && input[i] <= '9')
- {
- newInput.Insert(p, (char)(input[i]));
- p++;
- }
- }
- string text = newInput.ToString();
- while ((input != "end" && input != "END") && lenght != 0)
- {
- arr.Add(text);
- messages++;
- break;
- }
- }
- else
- {
- break;
- }
- }
- } while (input != "end" || input != "END");
- if (messages == 0)
- {
- Console.WriteLine("No message received.");
- }
- else
- {
- Console.WriteLine("Total number of messages: {0}", messages);
- }
- for (int k = 0; k < arr.Count; k++)
- {
- string newInputt = arr[k].ToString();
- newInputt = Reverse(newInputt);
- Console.WriteLine(newInputt);
- }
- }
- public static string Reverse(string str)
- {
- char[] chars = str.ToCharArray();
- for (int i = 0, j = str.Length - 1; i < j; i++, j--)
- {
- char c = chars[i];
- chars[i] = chars[j];
- chars[j] = c;
- }
- return new string(chars);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement