anizko

Message Decrypter

Aug 7th, 2019
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.35 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using System.Text;
  4. namespace problem02
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int countOfMessages = int.Parse(Console.ReadLine());
  11.             string pattern = @"^(\$|%)([A-Z][a-z]{2,})\1: \[(\d+)\]\|\[(\d+)\]\|\[(\d+)\]\|$";
  12.            
  13.  
  14.             for (int i = 0; i < countOfMessages; i++)
  15.             {
  16.                 string text = Console.ReadLine();
  17.                 Match match = Regex.Match(text,pattern);
  18.  
  19.                 if(match.Length>0)
  20.                 {
  21.                     string name = match.Groups[2].Value;
  22.                     char firstSymbol = (char)int.Parse(match.Groups[3].Value);
  23.                     char secondSymbol = (char)int.Parse(match.Groups[4].Value);
  24.                     char triedSymbol = (char)int.Parse(match.Groups[5].Value);
  25.                     StringBuilder message = new StringBuilder();
  26.                     message.Append(firstSymbol);
  27.                     message.Append(secondSymbol);
  28.                     message.Append(triedSymbol);
  29.                     Console.WriteLine($"{name}: {message}");
  30.                 }
  31.                 else
  32.                 {
  33.                     Console.WriteLine("Valid message not found!");
  34.                 }
  35.                
  36.             }
  37.            
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment