This was a fun way to kill an hour - thanks!


using System;

using System.Collections.Generic;

#endregion

namespace IsPalindrome

{

  using System.Globalization;

  using System.Text;

  class Program

  {

    static void Main(string[] args)

    {

      Program program = new Program();

      Console.WriteLine("IsPalindrome method passes: " + program.testIsPalindrome().ToString());

    }

    bool IsPalindrome(String candidate)

    {

      return IsPalindrome(candidate, CultureInfo.CurrentCulture);

    }

/// <summary>

/// From m-w.com, definition of Palindrome: a word, verse, or sentence (as "Able was I ere I saw Elba") or a number (as 1881) that reads the same backward or forward.

/// For the purposes of this method, a palindrome is a string whose component characters "read" the same

/// forwards or backwards, ignoring whitespace, punctuation, and control. Whether it means anything in the

/// target language is, unfortunately, ignored! But it should work with strings of any culture.

/// A null culture is assumed to mean the current culture.

/// A null or zero-length candidate string is not considered a palindrome.

/// </summary>

/// <returns></returns>

    bool IsPalindrome(String candidate, CultureInfo culture)

    {

      if (null == candidate)

        return false; // null candidates are not palindromes.

      if (candidate.Length == 0)

        return false; // zero-length candidates are not palindromes

      if (null == culture)

        culture = CultureInfo.CurrentCulture; // use the default if a null was provided.

      StringBuilder blackStringB = new StringBuilder(candidate.Length); // black string; no whitespace.

// remove whitespace, punctuation, control characters; they don't count towards palindromes.

      for (int i = 0; i < candidate.Length; i++)

      {

        if (!Char.IsWhiteSpace(candidate, i)

&& !Char.IsPunctuation(candidate, i)

&& !Char.IsControl(candidate, i))

          blackStringB.Append(candidate, i, 1);

      }

// now we have no whitespace.

      if (blackStringB.Length == 0)

        return false; // zero-length candidates are not palindromes

      String blackString = blackStringB.ToString().ToUpper(culture);

// we've removed case sensitivity by converting all characters to upper case.

      int length = blackString.Length;

      for (int i = 0; i < length / 2; i++)

      {

        if (0 != blackString[i].CompareTo(blackString[(length - i) - 1]))

        return false;

      }

      return true;

    }

    bool testIsPalindrome()

    {

// palindromes

    if (!IsPalindrome("a", CultureInfo.CurrentCulture))

      return false;

    if (!IsPalindrome("aA", CultureInfo.CurrentCulture))

      return false;

    if (!IsPalindrome("aa", CultureInfo.CurrentCulture))

      return false;

    if (!IsPalindrome("aaa", CultureInfo.CurrentCulture))

      return false;

    if (!IsPalindrome("2a7a2", CultureInfo.CurrentCulture))

      return false;

    if (!IsPalindrome("aaaa", CultureInfo.CurrentCulture))

      return false;

    if (!IsPalindrome("aBa", CultureInfo.CurrentCulture))

      return false;

    if (!IsPalindrome("a Ba", CultureInfo.CurrentCulture))

      return false;

    if (!IsPalindrome("aB a", CultureInfo.CurrentCulture))

      return false;

    if (!IsPalindrome("a.Ba?", CultureInfo.CurrentCulture))

      return false;

    if (!IsPalindrome("A B?; a?", CultureInfo.CurrentCulture))

      return false;

    if (!IsPalindrome("Able was I ere I saw Elba", CultureInfo.CurrentCulture)) // Napoleon, we hardly knew ye.

      return false;

    if (!IsPalindrome("A man, a plan, a canal: Panama!", CultureInfo.CurrentCulture))

      return false;

    StringBuilder sb = new StringBuilder();

    sb.Append(Char.MinValue);

    sb.Append(Char.MaxValue);

    sb.Append(Char.MinValue);

    if (!IsPalindrome(sb.ToString(), CultureInfo.CurrentCulture))

      return false;

    if (!IsPalindrome(GeneratePalindrome()))

      return false;

    if (!IsPalindrome(GeneratePalindrome()))

      return false;

    if (!IsPalindrome(GeneratePalindrome()))

      return false;

// not palindromes

    if (IsPalindrome("oiiunrviuhnuiycgcbzsrucgckjzsehbcuycu33ndjdjdj", CultureInfo.CurrentCulture))

      return false;

    if (IsPalindrome("67__32++7S'", CultureInfo.CurrentCulture))

      return false;

    if (IsPalindrome("ab", CultureInfo.CurrentCulture))

      return false;

    if (IsPalindrome("aB", CultureInfo.CurrentCulture))

      return false;

    if (IsPalindrome("", CultureInfo.CurrentCulture))

      return false;

    if (IsPalindrome(null, CultureInfo.CurrentCulture))

      return false;

    if (IsPalindrome("A man, a plan, a canal: Suez?", CultureInfo.CurrentCulture))

      return false;

    sb = new StringBuilder(); // check a string that can't be a palindrome with end chars.

    sb.Append(Char.MinValue);

    sb.Append(Char.MaxValue);

    sb.Append('a');

    sb.Append('a');

    sb.Append(Char.MinValue);

    sb.Append('a');

    if (IsPalindrome(sb.ToString(), CultureInfo.CurrentCulture))

      return false;

    return true;

    }

/// <summary>

/// A method for testing IsPalindrome, which brings forth the pressing question: who's testing GeneratePalindrome?

/// </summary>

  string GeneratePalindrome()

  {

    Random random = new Random();

    int halfLength = random.Next(80);

    char[] buffer = new char[halfLength * 2];

    for (int i = 0; i < halfLength; i++)

    {

      buffer[i] = (char)(random.Next(Char.MinValue, Char.MaxValue));

      buffer[buffer.Length - 1 - i] = buffer[i];

    }

    return new string(buffer);

    }

  }

}