Advertisement
elena1234

Extract Palindromes-with RegEx and Reverse()

Dec 7th, 2020 (edited)
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.94 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5.  
  6. namespace ExtractPalindromes
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)      
  11.         {
  12.             string input=Console.ReadLine();
  13.             var list = new List<string>();
  14.             Regex regexForWord = new Regex(@"[a-zA-Z]+");
  15.             MatchCollection words = regexForWord.Matches(input);
  16.             foreach (Match match in words)
  17.             {
  18.                 string currentWord = match.Value;
  19.                 char [] reverse =currentWord.Reverse().ToArray(); //char Array
  20.                 string reverseCurrentWord = (string.Join("", reverse)).ToString();
  21.  
  22.                 if (currentWord == reverseCurrentWord)
  23.                 {
  24.                     list.Add(currentWord);
  25.                 }
  26.             }
  27.  
  28.             Console.WriteLine(string.Join(", ",list));          
  29.         }
  30.     }
  31. }
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement