Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApplication7
- {
- class Program
- {
- static void Main(string[] args)
- {
- string input = Console.ReadLine();
- Console.WriteLine(ExtractPalindromes(input));
- }
- static string ExtractPalindromes(string input)
- {
- string[] arrayInput = input.Split(new string[] { "?", " ", "!", ",", "." }, StringSplitOptions.RemoveEmptyEntries);
- List<string> palindromes = new List<string>();
- foreach (var word in arrayInput)
- {
- string reversedWord = ReverseString(word);
- if (word == reversedWord)
- {
- palindromes.Add(word);
- }
- }
- palindromes.Sort();
- string output = string.Join(", ", palindromes);
- return output;
- }
- static string ReverseString(string someString)
- {
- StringBuilder reverse = new StringBuilder();
- for (int i = someString.Length - 1; i >= 0; i--)
- {
- reverse.Append(someString[i]);
- }
- return reverse.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment