Advertisement
Guest User

Vili

a guest
Feb 12th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.78 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace DetectiveBoev
  8. {
  9.     class DetectiveBoev
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string secretWord = Console.ReadLine(); // We create a console readable variable for the secret word.
  14.             string encryptedMsg = Console.ReadLine(); // We create a console readable variable for the encrypted message.
  15.             int wordSum = 0; // Create a int var to hold the sum from each char in the secret word.
  16.             int mask = 0; // The int var,named mask holds the sum of the sum of chars in the secret word.
  17.             string decryptedMsg = ""; // This string var holds empty value of the decrypted message,which we'll use later.
  18.             for (int i = 0; i < secretWord.Length; i++) // This for loop gets the sum of the chars in the secret word and ends when we get to the end of secret word.
  19.             {
  20.                 wordSum += secretWord[i]; // The wordSum holds the value of the each symbol in the char array of secretWord. e.g: secretWord = "code",then wordSum  = 99 + 111 + 100 + 101 = 411.
  21.             }
  22.             while (wordSum > 9) // 411 > 9; Entering in the loop.
  23.             {
  24.                 while (wordSum > 0) //411 > 0; Entering in the inner loop.
  25.                 {
  26.                     mask += wordSum % 10; // The mask is 0,so we take the value of the wordSum and divide by 10 with reminder and add it to the mask.
  27.                     wordSum /= 10; // Divide wordSum by 10.
  28.                 }
  29.                 wordSum = mask; //The value of wordSum is now equal to value of the mask.
  30.             }
  31.             for (int i = 0; i < encryptedMsg.Length; i++) // Make 2nd for loop to decrypt the messige.
  32.             {
  33.                 if (encryptedMsg[i] % mask == 0) // We make if statement to check every symbol in the char array of the encryptedMsg is dividing by the mask without reminder.
  34.                 {
  35.                     decryptedMsg += (char)(mask + encryptedMsg[i]); // If it's so. We apply the value of the CASTED to char encryptedMsg symbol of the array + mask.
  36.                 }
  37.                 else // if not.
  38.                 {
  39.                     decryptedMsg += (char)(encryptedMsg[i] - mask); // We apply the value ot the CASTED to char encrptedMsg symbol of the array - mask.
  40.                 }
  41.             }
  42.             for (int i = decryptedMsg.Length - 1; i >= 0; i--) // This is the loop that reversing the message. The i has a value of the decryptedMsg - 1,and we stop the loop when i has a value of 0 or higher.
  43.             {
  44.                 Console.Write(decryptedMsg[i]); // This print one char of decryptedMsg each loop.
  45.  
  46.             }
  47.             Console.WriteLine();
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement