ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

IsPalindrome

Checks to see if the given text is a valid palindrome or not.

Source

public static bool IsPalindrome(this string word)
{
	int nLen, nHalfLen;
	bool bValid = true;

	nLen = word.Length - 1;
	nHalfLen = nLen / 2;
	for (int i = 0; i < nHalfLen; i++)
	{
		if (word.Substring(i, 1) != word.Substring(nLen - i, 1)) bValid = false;
	}

	return bValid;
}

Example

Console.WriteLine("Is Palindrome: {0}", "earljon".IsPalindrome()); // returns False
Console.WriteLine("Is Palindrome: {0}", "1234321".IsPalindrome()); // returns True

Author: Earljon Hidalgo

Submitted on: 26 mrt. 2008

Language: C#

Type: System.Boolean

Views: 5799