Advertisement
msmilkoff

Palindromes

Sep 27th, 2015
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. class Palindromes
  7. {
  8. static void Main()
  9. {
  10. var text = Console.ReadLine()
  11. .Split(new char[] { ' ', ',', '.', '?', '!' }, StringSplitOptions.RemoveEmptyEntries)
  12. .ToArray();
  13.  
  14. List<string> palindromes = new List<string>();
  15. for (int i = 0; i < text.Length; i++)
  16. {
  17. if (IsPalindrome(text[i]))
  18. {
  19. palindromes.Add(text[i]);
  20.  
  21. }
  22. }
  23. palindromes.Sort();
  24. var uniquePalindromes = new HashSet<string>(palindromes);
  25. Console.Write(string.Join(", ", uniquePalindromes));
  26. Console.WriteLine();
  27. }
  28. static bool IsPalindrome(string word)
  29. {
  30. int leftIndex = 0;
  31. int rightIndex = word.Length - 1;
  32. while (true)
  33. {
  34. if (leftIndex > rightIndex)
  35. {
  36. return true;
  37. }
  38.  
  39. char leftChar = word[leftIndex];
  40. char rightChar = word[rightIndex];
  41. if (leftChar != rightChar)
  42. {
  43. return false;
  44. }
  45. leftIndex++;
  46. rightIndex--;
  47. }
  48.  
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement