Advertisement
Guest User

Untitled

a guest
Aug 15th, 2013
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.54 KB | None | 0 0
  1. /*Write a program that extracts from a given text all palindromes, e.g. "ABBA", "lamal", "exe".*/
  2. using System;
  3.  
  4. class Palindromes
  5. {
  6.     public static bool IsPalindrome(string value)
  7.     {
  8.         bool isPalindrome = false;
  9.         char[] symbols = value.ToCharArray();
  10.         int length = symbols.Length / 2;// I divide the word in two parts
  11.         string leftSide = "";
  12.         string rightSide = "";
  13.  
  14.         for (int i = 0; i < length; i++)
  15.         {
  16.             leftSide += symbols[i].ToString();
  17.         }
  18.  
  19.         if (length % 2 == 0)
  20.         {
  21.             for (int i = symbols.Length - 1; i >= length; i--)
  22.             {
  23.                 rightSide += symbols[i];
  24.             }
  25.         }
  26.         else
  27.         {
  28.             for (int i = symbols.Length - 1; i > length; i--)
  29.             {
  30.                 rightSide += symbols[i];
  31.             }
  32.         }
  33.  
  34.         if (leftSide == rightSide)
  35.         {
  36.             isPalindrome = true;
  37.         }
  38.         else
  39.         {
  40.             isPalindrome = false;
  41.         }
  42.         return isPalindrome;
  43.     }
  44.  
  45.     static void Main()
  46.     {
  47.         string text = "hello csharp exe,here is the bob phone caller!";
  48.         string[] textWithoutSigns = text.Split(new char[] { ' ', '?', '!', ';', ',', '\n', '\t', '\r', '.', '-', '_', '[', ']', '{', '}', }, StringSplitOptions.RemoveEmptyEntries);
  49.         foreach (var word in textWithoutSigns)
  50.         {
  51.             if (IsPalindrome(word) == true)
  52.             {
  53.                 Console.WriteLine(word);
  54.             }
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement