petarkobakov

Mirror words

Aug 11th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Text.RegularExpressions;
  4. using System.Collections.Generic;
  5.  
  6. namespace Mirror_Words
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. string input = Console.ReadLine();
  13.  
  14. Regex pattern = new Regex(@"([\#\@]{1})(?<firstword>[A-z]{3,})(\1)([\#\@]{1})(?<secondword>[A-z]{3,})(\1)");
  15. MatchCollection match = pattern.Matches(input);
  16. List<string> pairs = new List<string>();
  17. int noMirrors = 0;
  18. int counter = 0;
  19.  
  20. if (!pattern.IsMatch(input))
  21. {
  22. Console.WriteLine("No word pairs found!");
  23. }
  24.  
  25. else
  26. {
  27. counter = match.Count;
  28. Console.WriteLine($"{counter} word pairs found!");
  29.  
  30. foreach (Match word in match)
  31. {
  32. string firstWord = word.Groups["firstword"].Value;
  33. string secondWord = word.Groups["secondword"].Value;
  34.  
  35. var myArray = firstWord.ToCharArray();
  36. Array.Reverse(myArray);
  37. string reversedFirstWord = string.Concat(myArray);
  38.  
  39.  
  40. if (reversedFirstWord.Equals(secondWord))
  41. {
  42.  
  43. string combination = $"{firstWord} <=> {secondWord}";
  44. pairs.Add(combination);
  45.  
  46. }
  47.  
  48. else
  49. {
  50. noMirrors++;
  51. }
  52.  
  53. }
  54.  
  55. }
  56.  
  57. if (noMirrors == counter)
  58. {
  59. Console.WriteLine("No mirror words!");
  60. }
  61.  
  62. else
  63. {
  64. Console.WriteLine("The mirror words are:");
  65. Console.WriteLine(string.Join(", ", pairs));
  66. }
  67.  
  68. }
  69. }
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment