Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- class ExtractPalindromes
- {
- static char[] GetPunctuationSigns()
- {
- List<char> allPunctiation = new List<char>();
- for (int i = 0; i < 128; i++)
- {
- if (char.IsPunctuation((char)i))
- {
- allPunctiation.Add((char)i);
- }
- }
- allPunctiation.Add((char)32);
- return allPunctiation.ToArray();
- }
- static void Main()
- {
- string str = @"Nice blue sky. No LaiaL flying in the sky. aABBAa will tot come in Bulgaria.";
- List<string> palindromes = new List<string>();
- string[] strArr = str.Split(GetPunctuationSigns(), StringSplitOptions.RemoveEmptyEntries);
- for (int i = 0; i < strArr.Length; i++)
- {
- int wordLength = strArr[i].Length;
- //Get lengths
- int halfLength = wordLength / 2;
- int righSideStartIndex = wordLength - halfLength;
- //Get Left and Right Side of word
- string leftSide = strArr[i].Substring(0, halfLength);
- string rightSide = strArr[i].Substring(righSideStartIndex, halfLength);
- //Prepare Right side for COMPARISON by Reversing Right side to look the same if they are ;)
- char[] rightSideRevArr = rightSide.ToCharArray();
- Array.Reverse(rightSideRevArr);
- string rightSideRev = new string(rightSideRevArr); //Here I have the string again
- //Compare both sides
- if (leftSide == rightSideRev)
- {
- palindromes.Add(strArr[i]);
- }
- }
- foreach (var item in palindromes)
- {
- Console.WriteLine(item);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement