Advertisement
Guest User

06. Palindromes

a guest
May 13th, 2015
388
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. class Palindromes
  8. {
  9.     static void Main()
  10.     {
  11.         string[] input = Console.ReadLine().Split(new char[] { ' ', ',', '.', '?', '!' }, StringSplitOptions.RemoveEmptyEntries);
  12.         List<string> palindromes = new List<string>();
  13.         for (int i = 0; i < input.Length; i++)
  14.         {
  15.             if (IsPalindrome(input[i]))
  16.             {
  17.                 palindromes.Add(input[i]);
  18.             }
  19.         }
  20.         palindromes.Sort();
  21.         Console.WriteLine(string.Join(", ", palindromes));
  22.     }
  23.  
  24.     static bool IsPalindrome(string str)
  25.     {
  26.         int min = 0;
  27.         int max = str.Length - 1;
  28.         while (true)
  29.         {
  30.             if (min > max)
  31.             {
  32.                 return true;
  33.             }
  34.             char char1 = str[min];
  35.             char char2 = str[max];
  36.  
  37.             if (!char1.Equals(char2))
  38.             {
  39.                 return false;
  40.             }
  41.             min++;
  42.             max--;
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement