Advertisement
obedmarsh

04. Palindromes

Aug 2nd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.06 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Extended
  7. {
  8.     class Program
  9.     {
  10.         public static bool IsPalindrome(string inp)
  11.         {
  12.             int min = 0;
  13.             int max = inp.Length - 1;
  14.             while (true)
  15.             {
  16.                 if (min > max)
  17.                 {
  18.                     return true;
  19.                 }
  20.                 char a = inp[min];
  21.                 char b = inp[max];
  22.                 if (a != b)
  23.                 {
  24.                     return false;
  25.                 }
  26.                 min++;
  27.                 max--;
  28.             }
  29.         }
  30.         static void Main(string[] args)
  31.         {
  32.             string[] input = Console.ReadLine().Split(new char[]{ ' ', ',', '!', '.', '?' }, StringSplitOptions.RemoveEmptyEntries);
  33.             List<string> l = input.Where(x => IsPalindrome(x) == true).Select(x => x).ToList();
  34.             l.Sort();
  35.             l = l.Distinct().ToList();
  36.             Console.WriteLine(string.Join(", ", l));
  37.         }
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement