Advertisement
Guest User

Palindromes

a guest
May 14th, 2015
590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.72 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Text.RegularExpressions;
  4.  
  5.     class Program
  6.     {
  7.     static void Main(string[] args)
  8.     {
  9.         // input
  10.         string input = Console.ReadLine();
  11.         MatchCollection MatchesWords = Regex.Matches(input, @"\b\w+\b");
  12.  
  13.         // regex matches to array
  14.         var matches = MatchesWords.Cast<Match>()
  15.             .Select(m => m.Value).OrderBy(m => m);
  16.  
  17.         // print with foreach
  18.         foreach (var word in matches)
  19.             if (Palindrome(word)) Console.WriteLine(word);
  20.         Console.WriteLine();
  21.     }
  22.  
  23.     static bool Palindrome(string word)
  24.     {
  25.     return Enumerable.SequenceEqual(word.ToCharArray(), word.ToCharArray().Reverse());
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement