LusienGG

[C#] String Palindromes

Jun 13th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.08 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _08.Palindromes
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             char[] separators = { ' ','.','?','!',',' };
  14.             string text = Console.ReadLine();
  15.             var words = text.Split(separators, StringSplitOptions.RemoveEmptyEntries).ToList().Distinct();
  16.             var resultSortedWords = new List<string>();
  17.             foreach (var w in words)
  18.             {
  19.                 bool isPalindrome = true;
  20.                 for (int i = 0; i < w.Length / 2; i++)
  21.                 {
  22.                     if (w[i] != w[w.Length - i - 1])
  23.                     {
  24.                         isPalindrome = false;
  25.                     }
  26.                 }
  27.                 if (isPalindrome)
  28.                 {
  29.                     resultSortedWords.Add(w);
  30.                 }
  31.             }
  32.             resultSortedWords.Sort();
  33.             Console.WriteLine(string.Join(", ",resultSortedWords));
  34.         }
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment