Advertisement
murkata86

Palindromes

Jun 2nd, 2016
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Pa_indromes
  6. {
  7.     class Program
  8.     {
  9.         static void Main()
  10.         {
  11.             string[] inputParams = Console.ReadLine().Split(new char[] { ' ', '\t', ',', '?', '!', '.' }, StringSplitOptions.RemoveEmptyEntries);
  12.  
  13.             List<string> palindromes = new List<string>();
  14.  
  15.             for (int i = 0; i < inputParams.Length; i++)
  16.             {
  17.                 string word = inputParams[i];
  18.  
  19.                 if (word.Length == 1)
  20.                 {
  21.                     palindromes.Add(word);
  22.                 } else if(word.Length % 2 == 1)
  23.                 {
  24.                     bool sidesMatches = true;
  25.  
  26.                     int middleCharacter = word.Length / 2;
  27.  
  28.                     for (int j = 0; j < middleCharacter; j++)
  29.                     {
  30.                         if (word[middleCharacter - (1 + j)] != word[middleCharacter + (1 + j)])
  31.                         {
  32.                             sidesMatches = false;
  33.                             break;
  34.                         }
  35.                     }
  36.  
  37.                     if (sidesMatches)
  38.                     {
  39.                         palindromes.Add(word);
  40.                     }
  41.                 } else
  42.                 {
  43.                     bool sidesMatches = true;
  44.  
  45.                     int middleCharacter = word.Length / 2;
  46.  
  47.                     for (int j = 0; j < middleCharacter; j++)
  48.                     {
  49.                         if (word[middleCharacter - (1 + j)] != word[middleCharacter + j])
  50.                         {
  51.                             sidesMatches = false;
  52.                             break;
  53.                         }
  54.                     }
  55.  
  56.                     if (sidesMatches)
  57.                     {
  58.                         palindromes.Add(word);
  59.                     }
  60.                 }
  61.             }
  62.  
  63.             palindromes.Sort((a, b) => a.CompareTo(b));
  64.  
  65.             Console.WriteLine("[" + string.Join(", ", palindromes) + "]");
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement