Advertisement
ivo_petkov01

02. Mirror Words

Mar 28th, 2023
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.57 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5.  
  6. namespace _02._Mirror_Words_2
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             string inputText = Console.ReadLine();
  13.  
  14.             string pattern = @"(\@|#)(?<first>[A-Za-z]{3,})\1\1(?<last>[A-Za-z]{3,})\1";
  15.  
  16.             MatchCollection matches = Regex.Matches(inputText, pattern);
  17.             List<string> mirrors = new List<string>();
  18.  
  19.             foreach (Match match in matches)
  20.             {
  21.                 if (match.Success)
  22.                 {
  23.                     string firstWord = match.Groups["first"].Value;
  24.                     string secondWord = match.Groups["last"].Value;
  25.                     string reversed = new string(secondWord.Reverse().ToArray());
  26.  
  27.                     if (reversed == firstWord)
  28.                     {
  29.                         mirrors.Add($"{firstWord} <=> {secondWord}");
  30.                     }
  31.                 }
  32.             }
  33.  
  34.             if (matches.Count == 0)
  35.             {
  36.                 Console.WriteLine("No word pairs found!");
  37.             }
  38.             else
  39.             {
  40.                 Console.WriteLine($"{matches.Count} word pairs found!");
  41.             }
  42.  
  43.             if (mirrors.Count == 0)
  44.             {
  45.                 Console.WriteLine("No mirror words!");
  46.             }
  47.             else
  48.             {
  49.                 Console.WriteLine("The mirror words are:");
  50.                 Console.WriteLine(string.Join(", ", mirrors));
  51.             }
  52.         }
  53.     }
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement