Created
January 5, 2013 05:11
-
-
Save gogsbread/4459886 to your computer and use it in GitHub Desktop.
Interesting Algorithm - Detect if a string is Palindrome IsPaliDrome()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Text.RegularExpressions; | |
| namespace RandomMusings | |
| { | |
| public class DetectPalidrome | |
| { | |
| public DetectPalidrome () | |
| { | |
| } | |
| static void Main () | |
| { | |
| string palidrome = "aibohphobia".Split(); | |
| //Console.WriteLine (IsPalidrome (palidrome)); | |
| //Console.WriteLine(IsPalidrome("Antony")); | |
| //Console.WriteLine(IsPalidrome("cammac")); | |
| Console.WriteLine(IsPalidrome("A Toyota. Race fast, safe car. A Toyota.")); | |
| } | |
| static bool IsPalidrome (string palidrome) | |
| { | |
| palidrome = palidrome.ToLower(); | |
| Regex re = new Regex(@"['\s?.,:]"); | |
| palidrome = re.Replace(palidrome,string.Empty); | |
| int head = 0; | |
| int tail = palidrome.Length - 1; | |
| bool isPali = true; | |
| while(head < tail){ | |
| if(palidrome[head] != palidrome[tail]){ | |
| isPali = false; | |
| break; | |
| } | |
| head++; | |
| tail--; | |
| } | |
| return isPali; | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment