Advertisement
simonradev

CubicMessages

Feb 16th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.05 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Text.RegularExpressions;
  4.  
  5. namespace CubicMessages
  6. {
  7.     public class CubicMessages
  8.     {
  9.         public static void Main()
  10.         {
  11.             StringBuilder result = new StringBuilder();
  12.  
  13.             Regex validateMessage = null;
  14.             string messageInfo = Console.ReadLine();
  15.             while (messageInfo != "Over!")
  16.             {
  17.                 int lenOfMessage = int.Parse(Console.ReadLine());
  18.  
  19.                 validateMessage = new Regex(
  20.                     @"^([0-9]+)([a-zA-Z]{" + lenOfMessage + @"})([^a-zA-Z]*)$");
  21.  
  22.                 if (!validateMessage.IsMatch(messageInfo))
  23.                 {
  24.                     messageInfo = Console.ReadLine();
  25.  
  26.                     continue;
  27.                 }
  28.  
  29.                 Match validMessage = validateMessage.Match(messageInfo);
  30.  
  31.                 string message = validMessage.Groups[2].ToString();
  32.                 string verificationCode = string.Concat(validMessage.Groups[1].ToString(),
  33.                                                             validMessage.Groups[3].ToString());
  34.  
  35.                 StringBuilder currResult = new StringBuilder();
  36.                 for (int currSymbol = 0; currSymbol < verificationCode.Length; currSymbol++)
  37.                 {
  38.                     char symbol = verificationCode[currSymbol];
  39.  
  40.                     if (!char.IsDigit(symbol))
  41.                     {
  42.                         continue;
  43.                     }
  44.  
  45.                     int index = int.Parse(symbol.ToString());
  46.  
  47.                     try
  48.                     {
  49.                         currResult.Append(message[index]);
  50.                     }
  51.                     catch (IndexOutOfRangeException)
  52.                     {
  53.                         currResult.Append(" ");
  54.                     }
  55.                 }
  56.  
  57.                 result.AppendLine($"{message} == {currResult}");
  58.  
  59.                 messageInfo = Console.ReadLine();
  60.             }
  61.  
  62.             Console.WriteLine(result.ToString().TrimEnd('\r', '\n'));
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement