Advertisement
North_Point

05. SoftUni Messages

Aug 7th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.51 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 _05.SoftUni_Messages
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             Regex messagePattern = new Regex(@"^\d+(?<massage>[A-Za-z]+)[^a-zA-Z]+$");
  15.             string input = Console.ReadLine();
  16.  
  17.             while (input != "Decrypt!")
  18.             {
  19.                 int validLength = int.Parse(Console.ReadLine());
  20.  
  21.                 if (messagePattern.IsMatch(input))
  22.                 {
  23.                     Match m = messagePattern.Match(input);
  24.                     string message = m.Groups["massage"].Value;
  25.  
  26.                     if (message.Length == validLength)
  27.                     {
  28.                         string decodedMassage = ADecodedMassage(input, message);
  29.                         Console.WriteLine("{1} = {0}",decodedMassage,message);
  30.                     }
  31.                    
  32.                 }
  33.  
  34.                 input = Console.ReadLine();
  35.             }
  36.         }
  37.         static string ADecodedMassage(string input , string massage)
  38.         {
  39.             string result = "";
  40.             foreach (var symbol in input)
  41.             {
  42.                 int index = symbol - '0';
  43.                 if (char.IsDigit(symbol) && index < massage.Length)
  44.                 {
  45.                     result += massage[symbol - '0'];
  46.                 }
  47.             }
  48.             return result;
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement