Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _04.Palindromes
- {
- public class Program
- {
- public static void Main()
- {
- var text = Console.ReadLine().Split(new char[] { ' ', ',', '.', '?', '!' }, StringSplitOptions.RemoveEmptyEntries);
- var palindromeWords = new List<string>();
- for (int i = 0; i < text.Length; i++)
- {
- if (IsPalindrome(text[i]))
- {
- palindromeWords.Add(text[i]);
- }
- }
- palindromeWords = palindromeWords.Distinct().ToList();
- Console.WriteLine(string.Join(", ", palindromeWords.OrderBy(p => p)));
- }
- public static bool IsPalindrome(string word)
- {
- var index = word.Length - 1;
- for (int i = 0; i < word.Length / 2; i++)
- {
- if (word[i] != word[index])
- {
- return false;
- }
- index--;
- }
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement