Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text.RegularExpressions;
- using System.Linq;
- namespace _02.MirrorWords
- {
- class Program
- {
- static void Main(string[] args)
- {
- string text = Console.ReadLine();
- string pattern = @"([@#])(?<firstword>[A-Za-z]{3,})\1{2}(?<secondword>[A-Za-z]{3,})\1";
- MatchCollection hiddenWordPairs = Regex.Matches(text, pattern);
- Dictionary<string, string> mirrorwords = new Dictionary<string, string>();
- int mirrorWordsCount = 0;
- foreach (Match wordPair in hiddenWordPairs)
- {
- string firstword = wordPair.Groups["firstword"].Value;
- string secondword = wordPair.Groups["secondword"].Value;
- string secondWordReversed = string.Empty;
- for (int i = secondword.Length - 1; i >= 0; i--)
- {
- char current = secondword[i];
- secondWordReversed += current;
- }
- if (firstword == secondWordReversed)
- {
- if (!mirrorwords.ContainsKey(firstword))
- {
- mirrorwords.Add(firstword, secondword);
- mirrorWordsCount++;
- }
- }
- }
- if (hiddenWordPairs.Count == 0)
- {
- Console.WriteLine($"No word pairs found!");
- }
- else if (hiddenWordPairs.Count > 0)
- {
- Console.WriteLine($"{hiddenWordPairs.Count} word pairs found!");
- }
- if (mirrorWordsCount == 0)
- {
- Console.WriteLine($"No mirror words!");
- }
- else if (mirrorWordsCount > 0)
- {
- Console.WriteLine("The mirror words are:");
- foreach (var kvp in mirrorwords)
- {
- Console.Write(string.Join(", ",$"{kvp.Key} <=> {kvp.Value}"));
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement