Advertisement
Aborigenius

SoftUni Messages

Aug 27th, 2017
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7.  
  8. namespace SoftuniMessages
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             string input = Console.ReadLine();
  15.             Regex pattern = new Regex(@"^\d+(?<message>[A-Za-z]+)[^a-zA-Z]+$");
  16.             while (input != "Decrypt!")
  17.             {
  18.                 int validMessageLenght = int.Parse(Console.ReadLine());
  19.  
  20.                 if (pattern.IsMatch(input))
  21.                 {
  22.                     Match m = pattern.Match(input);
  23.                     string message = m.Groups["message"].Value;
  24.  
  25.                     if (message.Length == validMessageLenght)
  26.                     {
  27.                         string decoded = DecodeMessage(input, message);
  28.  
  29.                         Console.WriteLine($"{message} = {decoded}");
  30.                     }
  31.                 }
  32.  
  33.                 input = Console.ReadLine();
  34.             }
  35.         }
  36.         static string DecodeMessage(string input, string message)
  37.         {
  38.             string result = String.Empty;
  39.             foreach (char symbol in input)
  40.             {
  41.                 int index = symbol - '0';
  42.                 if (char.IsDigit(symbol) && index < message.Length)
  43.                 {
  44.                     result += message[symbol - '0'];
  45.                 }
  46.             }
  47.             return result;
  48.         }
  49.     }
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement