Advertisement
zornitsa_zlateva

2.MirrorWords

Nov 27th, 2021
1,290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.09 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4. using System.Linq;
  5.  
  6. namespace _02.MirrorWords
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             string text = Console.ReadLine();
  13.  
  14.             string pattern = @"([@#])(?<firstword>[A-Za-z]{3,})\1{2}(?<secondword>[A-Za-z]{3,})\1";
  15.  
  16.             MatchCollection hiddenWordPairs = Regex.Matches(text, pattern);
  17.             Dictionary<string, string> mirrorwords = new Dictionary<string, string>();
  18.             int mirrorWordsCount = 0;
  19.  
  20.             foreach (Match wordPair in hiddenWordPairs)
  21.             {
  22.                 string firstword = wordPair.Groups["firstword"].Value;
  23.                 string secondword = wordPair.Groups["secondword"].Value;
  24.                 string secondWordReversed = string.Empty;
  25.  
  26.                 for (int i = secondword.Length - 1; i >= 0; i--)
  27.                 {
  28.                     char current = secondword[i];
  29.                     secondWordReversed += current;
  30.                 }
  31.                 if (firstword == secondWordReversed)
  32.                 {
  33.                     if (!mirrorwords.ContainsKey(firstword))
  34.                     {
  35.                         mirrorwords.Add(firstword, secondword);
  36.                         mirrorWordsCount++;
  37.                     }
  38.                 }
  39.             }
  40.             if (hiddenWordPairs.Count == 0)
  41.             {
  42.                 Console.WriteLine($"No word pairs found!");
  43.             }
  44.             else if (hiddenWordPairs.Count > 0)
  45.             {
  46.                 Console.WriteLine($"{hiddenWordPairs.Count} word pairs found!");
  47.             }
  48.             if (mirrorWordsCount == 0)
  49.             {
  50.                 Console.WriteLine($"No mirror words!");
  51.             }
  52.             else if (mirrorWordsCount > 0)
  53.             {
  54.                 Console.WriteLine("The mirror words are:");
  55.  
  56.                 foreach (var kvp in mirrorwords)
  57.                 {
  58.                     Console.Write(string.Join(", ",$"{kvp.Key} <=> {kvp.Value}"));
  59.                 }
  60.             }
  61.         }
  62.     }
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement