stefan1919

Palindromes

Sep 27th, 2015
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.32 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. namespace ConsoleApplication7
  8. {
  9.     class Program
  10.     {
  11.  
  12.         static void Main(string[] args)
  13.         {
  14.             string input = Console.ReadLine();
  15.             Console.WriteLine(ExtractPalindromes(input));
  16.         }
  17.        static string ExtractPalindromes(string input)
  18.         {
  19.             string[] arrayInput = input.Split(new string[] { "?", " ", "!", ",", "." }, StringSplitOptions.RemoveEmptyEntries);
  20.             List<string> palindromes = new List<string>();
  21.             foreach (var word in arrayInput)
  22.             {
  23.                 string reversedWord = ReverseString(word);
  24.                 if (word == reversedWord)
  25.                 {
  26.                     palindromes.Add(word);
  27.                 }
  28.             }
  29.             palindromes.Sort();
  30.             string output = string.Join(", ", palindromes);
  31.             return output;
  32.              
  33.         }
  34.        static string ReverseString(string someString)
  35.        {
  36.            StringBuilder reverse = new StringBuilder();
  37.            for (int i = someString.Length - 1; i >= 0; i--)
  38.            {
  39.                reverse.Append(someString[i]);
  40.            }
  41.            return reverse.ToString();
  42.        }
  43.        
  44.    
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment