Advertisement
Guest User

Untitled

a guest
Apr 10th, 2020
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.55 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.  
  13.             string pattern = @"([@#+])([A-Za-z]{3,})(\1)(\1)([A-Za-z]{3,})(\1)";
  14.             List<string> couples = new List<string>();
  15.  
  16.             MatchCollection matches = Regex.Matches(input, pattern);
  17.  
  18.             if (matches.Count <= 0)
  19.             {
  20.                 Console.WriteLine("No word pairs found!");
  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.  
  37.             if (matches.Count != 0)
  38.             {
  39.                 Console.WriteLine($"{matches.Count} word pairs found!");
  40.                 if (couples.Count != 0)
  41.                 {
  42.                 Console.WriteLine($"The mirror words are:");
  43.                 Console.WriteLine(string.Join(", ", couples));
  44.  
  45.                 }
  46.  
  47.             }
  48.             if (couples.Count == 0)
  49.             {
  50.                 Console.WriteLine("No mirror words!");
  51.             }
  52.  
  53.  
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement