Click here to Skip to main content
12,447,786 members (63,029 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

12.4K views
4 downloads
2 bookmarked
Posted

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

, 6 Feb 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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):

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:

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)

Share

About the Author

AspDotNetDev
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):

You may also be interested in...

Comments and Discussions

 
QuestionI found a three nice solutions here Pin
DARSHAN MODANI30-Mar-12 7:53
memberDARSHAN MODANI30-Mar-12 7:53 
GeneralRe: For case-sensitive ordinal comparisons, you can use: if (str... Pin
Richard Deeming17-Feb-11 3:15
memberRichard Deeming17-Feb-11 3:15 
GeneralReason for my vote of 2 My criticism: Creates two new string... Pin
OldWiseLlama16-Feb-11 9:12
memberOldWiseLlama16-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
memberCJ_Buck8-Feb-11 3:44 
GeneralReason for my vote of 5 Very efficient algorithm. Pin
ChantiPDM7-Feb-11 20:18
memberChantiPDM7-Feb-11 20:18 
Generalwhat if str.Length isn't an even number? Pin
jim lahey4-Feb-11 5:44
memberjim 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 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160811.3 | Last Updated 7 Feb 2011
Article Copyright 2011 by AspDotNetDev
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid