Advertisement
mustafov

Encrypt the Messages

Sep 3rd, 2015
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.03 KB | None | 0 0
  1. // Encrypt the Messages
  2. //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.
  3. //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.”
  4. //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.
  5. //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”
  6.  
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12.  
  13. namespace ConsoleApplication5
  14. {
  15.     class Program
  16.     {
  17.         static void Main(string[] args)
  18.         {
  19.             string input = Console.ReadLine();
  20.             string command = Console.ReadLine();
  21.             StringBuilder digits = new StringBuilder();
  22.             string result = null ;
  23.             for (int i = 0; i < input.Length; i++)
  24.             {
  25.                 int ascii = input[i];
  26.                 int lastDigit = ascii % 10;
  27.                 result = digits.Insert(i, lastDigit).ToString();
  28.             }
  29.             int number = 0;
  30.             StringBuilder final = new StringBuilder ();
  31.             string finalResult = null;
  32.             int index = 0;
  33.             for (int j = 0; j < result.Length;j++)
  34.             {
  35.                 if ((result[j] - '0') % 2 == 0)
  36.                 {
  37.                     number = (result[j] - '0') * (result[j] - '0');
  38.                 }
  39.                 else if ((result[j] - '0') % 2 != 0 && j != 0 && j != (result.Length - 1))
  40.                 {
  41.                     number = (result[j] - '0') + (result[j - 1] - '0') + (result[j + 1] - '0');
  42.                 }
  43.                 else if ((result[j] - '0') % 2 != 0 && j == 0 && input.Length == 1)
  44.                 {
  45.                     number = (result[j] - '0') + 0 + 0;
  46.                 }
  47.                 else if ((result[j] - '0') % 2 != 0 && j == 0)
  48.                 {
  49.                     number = (result[j] - '0') + 0 + (result[j + 1]- '0');
  50.                 }
  51.                 else if ((result[j] - '0') % 2 != 0 && j == (result.Length - 1))
  52.                 {
  53.                     number = (result[j] - '0') + 0 + (result[j - 1] - '0');
  54.                 }
  55.                 if (number > 9)
  56.                 {
  57.                     finalResult = final.Insert(index, number).ToString();
  58.                     index+=2;
  59.                 }
  60.                 else
  61.                 {
  62.                     finalResult = final.Insert(index, number).ToString();
  63.                     index++;
  64.                 }
  65.             }
  66.             if (command == "\\")
  67.             {
  68.                 for (int f = 0; f < finalResult.Length; f++)
  69.                 {
  70.                     for (int s = 0; s < finalResult.Length; s++)
  71.                     {
  72.                         if (f == s)
  73.                         {
  74.                             Console.Write(finalResult[f] + " ");
  75.                         }
  76.                         else
  77.                         {
  78.                             Console.Write("0" + " ");
  79.                         }
  80.                     }
  81.                     Console.WriteLine();
  82.                 }
  83.             }
  84.             else if (command == "/")
  85.             {
  86.                 int lett = finalResult.Length - 1;
  87.                 for (int f = 0; f < finalResult.Length; f++)
  88.                 {
  89.                     for (int s = finalResult.Length - 1; s >= 0; s--)
  90.                     {
  91.                         if (f == s)
  92.                         {
  93.                             Console.Write(finalResult[lett] + " ");
  94.                             lett--;
  95.                         }
  96.                         else
  97.                         {
  98.                             Console.Write("0" + " ");
  99.                         }
  100.                     }
  101.                     Console.WriteLine();
  102.                 }
  103.             }
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement