Advertisement
social1986

Untitled

Oct 23rd, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _04.Palindromes
  6. {
  7. public class Program
  8. {
  9. public static void Main()
  10. {
  11. var text = Console.ReadLine().Split(new char[] { ' ', ',', '.', '?', '!' }, StringSplitOptions.RemoveEmptyEntries);
  12. var palindromeWords = new List<string>();
  13.  
  14. for (int i = 0; i < text.Length; i++)
  15. {
  16. if (IsPalindrome(text[i]))
  17. {
  18. palindromeWords.Add(text[i]);
  19. }
  20. }
  21. palindromeWords = palindromeWords.Distinct().ToList();
  22. Console.WriteLine(string.Join(", ", palindromeWords.OrderBy(p => p)));
  23. }
  24.  
  25. public static bool IsPalindrome(string word)
  26. {
  27. var index = word.Length - 1;
  28.  
  29. for (int i = 0; i < word.Length / 2; i++)
  30. {
  31. if (word[i] != word[index])
  32. {
  33. return false;
  34. }
  35. index--;
  36. }
  37. return true;
  38. }
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement