Click here to Skip to main content
15,901,284 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.00/5 (3 votes)
6 Feb 2011CPOL 23.1K   2   8
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 <...
I prefer this technique (uses less memory and may be faster, but requires slightly more code):

C#
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 < halfway; i++)
    {
        if (!str.Substring(i, 1).Equals(str.Substring(lastIndex - i, 1), comparisonType))
        {
            valid = false;
            break;
        }
    }
    return valid;
}


You can then provide an overload to avoid passing in the comparison type:

C#
public bool IsPalindrome(string str)
{
    return IsPalindrome(str, StringComparison.OrdinalIgnoreCase);
}

License

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


Written By
Web Developer
United States United States

  • Managing Your JavaScript Library in ASP.NET (if you work with ASP.net and you don't read that, you are dead to me).
  • Graduated summa cum laude with a BS in Computer Science.
  • Wrote some articles and some tips.
  • DDR ("New high score? What does that mean? Did I break it?"), ping pong, and volleyball enthusiast.
  • Software I have donated to (you should too):

Comments and Discussions

 
QuestionI found a three nice solutions here Pin
DARSHAN MODANI30-Mar-12 7:53
DARSHAN MODANI30-Mar-12 7:53 
GeneralRe: For case-sensitive ordinal comparisons, you can use: if (str... Pin
Richard Deeming17-Feb-11 3:15
mveRichard Deeming17-Feb-11 3:15 
GeneralReason for my vote of 2 My criticism: Creates two new string... Pin
OldWiseLlama16-Feb-11 9:12
OldWiseLlama16-Feb-11 9:12 
GeneralRe: So? It also creates a variable, i, yet you didn't feel the n... Pin
AspDotNetDev16-Feb-11 9:42
protectorAspDotNetDev16-Feb-11 9:42 
GeneralReason for my vote of 5 smaller memory foot-print; jumps out... Pin
CJ_Buck8-Feb-11 3:44
CJ_Buck8-Feb-11 3:44 
GeneralReason for my vote of 5 Very efficient algorithm. Pin
Björn Friedrich7-Feb-11 20:18
Björn Friedrich7-Feb-11 20:18 
Generalwhat if str.Length isn't an even number? Pin
jim lahey4-Feb-11 5:44
jim lahey4-Feb-11 5:44 
GeneralRe: It will work fine in that case. Say str.Length is 7. 7 / 2 i... Pin
AspDotNetDev4-Feb-11 6:48
protectorAspDotNetDev4-Feb-11 6:48 
It will work fine in that case. Say str.Length is 7. 7 / 2 is 3.5. Since the result must be an integer, it will automatically be rounded down to 3. So the first 3 characters will be checked against the last 3 characters. The middle character does not need to be checked because of course it will be identical to itself.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.