ellapt

T14.20.ExtractPalindroms

Feb 3rd, 2013
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. using System;
  2.  
  3. class ExtractPalindroms
  4. {
  5. static void Main()
  6. {
  7. Console.WriteLine("Extract from a given text all palindromes\n");
  8.  
  9. string text = "Is lamal a camel? I listen to the ABBA songs with pleasure and wow,\nI have to write some exe, then go to the swimminglocolgnimmiws";
  10. Console.WriteLine("Original text:\n{0}\n", text);
  11. char[] splitSymbols = { ' ', '.', ',', '!', '\r', '\n' };
  12. string[] words = text.Split(splitSymbols, StringSplitOptions.RemoveEmptyEntries);
  13.  
  14.  
  15. foreach (string word in words)
  16. {
  17. bool palindrome = true;
  18. int halfLength=word.Length / 2;
  19. for (int i = 0; i < halfLength; i++)
  20. {
  21. if (word[i] != word[word.Length-1-i])
  22. {
  23. palindrome = false;
  24. break;
  25. }
  26. }
  27. if (palindrome&&word.Length>1)
  28. {
  29. Console.WriteLine(word);
  30. }
  31. }
  32. Console.WriteLine();
  33. }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment