Skip to content

Instantly share code, notes, and snippets.

@gogsbread
Created January 5, 2013 05:11
Show Gist options
  • Select an option

  • Save gogsbread/4459886 to your computer and use it in GitHub Desktop.

Select an option

Save gogsbread/4459886 to your computer and use it in GitHub Desktop.
Interesting Algorithm - Detect if a string is Palindrome IsPaliDrome()
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