Thread: palindrome function

  1. #1
    Unregistered
    Guest

    palindrome function

    ok here is how i figure i would do it...

    get input
    determine lenght of string and reverse it bu looping a decreasing for loop and concatenating the strings into a second string
    eg: text[100] added to reversedstring then text[99] added to reverstring and so on...

    then i would use the strcmp function to test for equality and see if it is indeed a palindrome... is this the best way to do it? anyone else have some faster or better pseusocode also i dont know how to determine the lenght of a string

  2. #2
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    use strlen(string) for length
    strcmp(str1,str2) for cmp

    yeah its fast but more mem will be used

    or you could use 2 loops one from 0 and incrementing and the other one from 99 and decrementing; comparing 0-99, 1-98 so on to see for palindrome.
    -

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    This has been handled many times before, just click on this to see.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Unregistered
    Guest
    ok no i have a litle problem the uncommented lines i do not understand... what does tolower do?

    Code:
    char* start = text; //pass the whole array into the start variable
       char* end = text + strlen(text) - 1; //pass the last charecter of the array into end
       while(end > start)
       {
           if(tolower(*start) != tolower(*end))
           {
               return(0);
           }
           end--;
           start++;
       }
       return(1);

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    tolower will return the upper case equivalent of the character that you pass to it.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM