Advertisement
ognyan

Untitled

Feb 1st, 2013
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.93 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class ExtractPalindromesFromText
  5. {
  6.     static void Main(string[] args)
  7.     {
  8.         string str = "Some text, ABBA alfa test txt bob soso potop php dhl";
  9.         string[] splitByWhitespace = str.Split(new char[]{' ', ',', '"'}, StringSplitOptions.RemoveEmptyEntries);
  10.         List<string> palindromes = new List<string>();
  11.         foreach (var word in splitByWhitespace)
  12.         {
  13.             bool isPalindrom = true;
  14.             for (int i = 0; i < word.Length / 2; i++)
  15.             {
  16.                 if (word[i] != word[word.Length - 1 - i])
  17.                 {
  18.                     isPalindrom = false;
  19.                     break;
  20.                 }
  21.             }
  22.             if (isPalindrom)
  23.             {
  24.                 palindromes.Add(word);
  25.             }
  26.         }
  27.         foreach (var words in palindromes)
  28.         {
  29.             Console.WriteLine(words);
  30.         }
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement