Click here to Skip to main content
11,494,174 members (60,159 online)
Click here to Skip to main content

Tagged as

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

, 6 Feb 2011 CPOL 3.3K 6 1
How about:public bool IsPalindrome(string stringToCheck){ char[] rev = stringToCheck.Reverse().ToArray(); return (stringToCheck.Equals(new string(rev), StringComparison.OrdinalIgnoreCase));}

Alternatives

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

6 Feb 2011
ashishkumar008
7 alternatives  
It's very easy to check string is palindrome or not. Be sure efficiency is good or not.
8 Mar 2011
intrueder
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...
15 Feb 2011
OldWiseLlama
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
AspDotNetDev
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,...
15 Feb 2011
UbaiSandouk
How about this:private bool IsPalindrome(string stringToCheck){ return stringToCheck.ToLower() == (new string(stringToCheck.Reverse().ToArray())).ToLower();}
16 Jan 2012
PoblarpBoonchuay
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; } ...

License

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

Share

About the Author

ZamirF

United States United States
No Biography provided

| | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 7 Feb 2011
Article Copyright 2011 by ZamirF
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid