Advertisement
Guest User

Untitled

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