Palindrome

This is a discussion on Palindrome within the C++ Programming forums, part of the General Programming Boards category; A program that is to check if a string is a palindrome. Every character must be put to lowercase. so ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    4

    Palindrome

    A program that is to check if a string is a palindrome.

    Every character must be put to lowercase.

    so if anyone wants to write some code, then by all means go ahead

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    22
    Sorry, but are that words that are the same when reversed?

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Write your code, then come back if you have specific problems with it, (+OS and compiler). If you want people to do your homework for you, then you'll get flamed.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    [HINT]
    No offense or anything, but a search of the board may help. [/HINT]

  5. #5
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    Try:

    bool palindrome = true; // Assume palindrome from start and test for non palindrome.

    for (i = 0; i < strlen (s) / 2; i++) // iterate first half of string.
    {
    if (s[i] != s[strlen (s) - i - 1])
    {
    palindrome = false;
    break;
    }

    }

    if (palindrome)
    printf ("YAY!!!!!");

    else
    printf ("NO!");

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    that's a bit of overkill dont you think?? how many times is strlen going to get called if the string is really big?? and that's not a case insensitive solution

    Nicholas35: i've already posted code to solve this problem in another thread.... before jumping on and asking people to do the code for you... have a search through the web, and the previous posts.. and you'll be amazed at what you find.

    Having said that.. since i'm in a good mood.. here's the code:

    Code:
    int Palindrome(char* text)
    {
       char* start = text;
       char* end = text + strlen(text) - 1;
       while(end > start)
       {
           if(tolower(*start) != tolower(*end))
           {
               return(0);
           }
           end--;
           start++;
       }
       return(1);
    }
    
    if(Palindrome(string))
    {
       cout << "This is a palindrome\n";
    }
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  7. #7
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    Yes, I hear you. That was just a quick and cheap solution (and it was late). Add the line:

    int stringLength;
    stringLength = strlen (s) / 2;

    And use that in the for loop.

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    Hey it's cool... i wasn't *****ing!

    strlen (s) / 2
    this isn't the line that's a problem.. the compiler will probably optimise that for you... the line that was the problem is :

    [i]if (s != s[strlen (s) - i - 1])
    this is the call that needs to be replaced

    Just simply making a point that there are better ways to do things than the obvious.

    i reckon there's 99.9% chance that there's a better solution than the one i wrote too!

    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error in Recursive String Palindrome Code
    By clegs in forum C Programming
    Replies: 13
    Last Post: 12-21-2008, 12:36 PM
  2. Is it a Palindrome?
    By xp5 in forum C Programming
    Replies: 3
    Last Post: 09-06-2007, 06:26 AM
  3. bool palindrome definition
    By justinc911 in forum C++ Programming
    Replies: 3
    Last Post: 11-26-2003, 05:50 PM
  4. Palindrome Coding trouble
    By TheLoneWolf32 in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2003, 07:05 PM
  5. palindrome HELP !!!
    By TED22_8 in forum C Programming
    Replies: 23
    Last Post: 01-22-2002, 02:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21