Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Encrypt 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. Your task is to write a program, which encrypts all messages in a //specific communication, prints them at the console as well as the total number of messages that have been sent.
- //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 //encrypted messages’ content and total count. Any non-empty string between the “start” and “end” keywords is considered a message. //If no messages have been sent between the “start” and the “end” keywords, you should print on the console: “No messages sent.”
- //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: ‘ ’ (space) is converted to a plus sign (‘ +’), ‘,’ is converted to ‘%’, ‘.’ is converted to ‘&’, //‘?’ is converted to ‘#’ and ‘!’ is converted to ‘$’. Digits (0-9) are not converted and stay the same.
- //For example, you receive the following message – “Secret message 1!” and you start encrypting it. Convert the 1st character ‘!’ //to ‘$’, then the 2nd character – ‘1’ stays the same, then covert the 3rd character – ‘ ’ to ‘+’, ‘e’ ’r’, ‘g’ ‘t’, ‘a’ ‘n’, //‘s’ ‘f’, ‘s’ ‘f’, ‘e’ ’r’ , ‘m’ ’z’, ‘ ’ ‘+’, ‘t’ ‘g’, ‘e’ ’r’ , ‘r’ ’e’ , ‘c’ ’p’ , ‘e’ ’r’ , ‘S’ ’F’. //After encrypting all letters, the message is: “Frperg+zrffntr+1$” and when you reverse it, you get the final encrypted message: //“$1+rtnffrz+greprF”
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApplication5
- {
- class Program
- {
- static void Main(string[] args)
- {
- string input = Console.ReadLine();
- string command = Console.ReadLine();
- StringBuilder digits = new StringBuilder();
- string result = null ;
- for (int i = 0; i < input.Length; i++)
- {
- int ascii = input[i];
- int lastDigit = ascii % 10;
- result = digits.Insert(i, lastDigit).ToString();
- }
- int number = 0;
- StringBuilder final = new StringBuilder ();
- string finalResult = null;
- int index = 0;
- for (int j = 0; j < result.Length;j++)
- {
- if ((result[j] - '0') % 2 == 0)
- {
- number = (result[j] - '0') * (result[j] - '0');
- }
- else if ((result[j] - '0') % 2 != 0 && j != 0 && j != (result.Length - 1))
- {
- number = (result[j] - '0') + (result[j - 1] - '0') + (result[j + 1] - '0');
- }
- else if ((result[j] - '0') % 2 != 0 && j == 0 && input.Length == 1)
- {
- number = (result[j] - '0') + 0 + 0;
- }
- else if ((result[j] - '0') % 2 != 0 && j == 0)
- {
- number = (result[j] - '0') + 0 + (result[j + 1]- '0');
- }
- else if ((result[j] - '0') % 2 != 0 && j == (result.Length - 1))
- {
- number = (result[j] - '0') + 0 + (result[j - 1] - '0');
- }
- if (number > 9)
- {
- finalResult = final.Insert(index, number).ToString();
- index+=2;
- }
- else
- {
- finalResult = final.Insert(index, number).ToString();
- index++;
- }
- }
- if (command == "\\")
- {
- for (int f = 0; f < finalResult.Length; f++)
- {
- for (int s = 0; s < finalResult.Length; s++)
- {
- if (f == s)
- {
- Console.Write(finalResult[f] + " ");
- }
- else
- {
- Console.Write("0" + " ");
- }
- }
- Console.WriteLine();
- }
- }
- else if (command == "/")
- {
- int lett = finalResult.Length - 1;
- for (int f = 0; f < finalResult.Length; f++)
- {
- for (int s = finalResult.Length - 1; s >= 0; s--)
- {
- if (f == s)
- {
- Console.Write(finalResult[lett] + " ");
- lett--;
- }
- else
- {
- Console.Write("0" + " ");
- }
- }
- Console.WriteLine();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement