dragonbs

Mirror Words

Mar 27th, 2023
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.21 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5.  
  6. class Program
  7. {
  8.     static void Main()
  9.     {
  10.         string input = Console.ReadLine();
  11.         string regex = @"([@#])(?<first>[A-Za-z]{3,})\1\1(?<second>[A-Za-z]{3,})\1";
  12.         MatchCollection matches = Regex.Matches(input, regex);
  13.         var results = new Dictionary<string, string>();
  14.  
  15.         foreach (Match match in matches)
  16.         {
  17.  
  18.             string first = match.Groups["first"].Value;
  19.             string second = match.Groups["second"].Value;
  20.             string secondReversed = string.Join("", second.ToCharArray().Reverse().ToArray());
  21.             if (first == secondReversed)
  22.                 results.Add(first, second);
  23.         }
  24.  
  25.         if (matches.Count == 0)
  26.             Console.WriteLine("No word pairs found!");
  27.         else
  28.             Console.WriteLine($"{matches.Count} word pairs found!");
  29.  
  30.         if (results.Count == 0)
  31.             Console.WriteLine("No mirror words!");
  32.         else
  33.         {
  34.             Console.WriteLine("The mirror words are:");
  35.             Console.WriteLine(String.Join(", ", results.Select(x => $"{x.Key} <=> {x.Value}")));
  36.         }
  37.     }
  38. }
Add Comment
Please, Sign In to add comment