Click here to Skip to main content
13,295,472 members (29,727 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 8 Mar 2011

To check string is palindrome or not in .NET (C#)

, 8 Mar 2011
You forgot about the non-alphabetic characters in a palindrome.A string like "Madam, in Eden I'm Adam" will produce False in your code, but it is a palindrome!Provide you my version. I also put it in extension method:public static class StringExtension{ public static bool...


Members may post updates or alternatives to this current article in order to show different approaches or add new features.

6 Feb 2011
7 alternatives  
It's very easy to check string is palindrome or not. Be sure efficiency is good or not.
15 Feb 2011
Yet another way to do this. This might not be as short the other alternates and might not look elegant, but at least it only needs two int variables and doesn't create any extra objects (reference types)bool IsPalindrome(string stringToCheck, bool ignoreCase) { if...
6 Feb 2011
I prefer this technique (uses less memory and may be faster, but requires slightly more code):public bool IsPalindrome(string str, StringComparison comparisonType){ bool valid = true; int halfway = str.Length / 2; int lastIndex = str.Length - 1; for (int i = 0; i <...
7 Feb 2011
Jeremy Hutchinson
There's no need to compare each character of the string separately:public static bool IsPalindrome(string str, StringComparison comparisonType){ if (string.IsNullOrEmpty(str)) return false; string str2 = new string(str.Reverse().ToArray()); return string.Equals(str, str2,...
6 Feb 2011
How about:public bool IsPalindrome(string stringToCheck){ char[] rev = stringToCheck.Reverse().ToArray(); return (stringToCheck.Equals(new string(rev), StringComparison.OrdinalIgnoreCase));}
15 Feb 2011
How about this:private bool IsPalindrome(string stringToCheck){ return stringToCheck.ToLower() == (new string(stringToCheck.Reverse().ToArray())).ToLower();}
16 Jan 2012
public bool isPalindrome(String str){ if (String.IsNullOrEmpty(str)) return false; int length = str.Length; str = str.ToLower(); for (int i = 0; i < (length / 2); i++) { if (str[i] != str[length - 1 - i]) return false; } ...


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer DevelopEx
Ukraine Ukraine
No Biography provided

| | Privacy | Terms of Use | Mobile
Web02 | 2.8.171207.1 | Last Updated 8 Mar 2011
Article Copyright 2011 by intrueder
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid