Test Palindrome

This is a discussion on Test Palindrome within the C Programming forums, part of the General Programming Boards category; HI, My goal is to write a program to test if a particular string is a palindrome (string that's spelled ...

  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    Test Palindrome

    HI,

    My goal is to write a program to test if a particular string is a palindrome (string that's spelled the same way backwards and forwards ). Here is the code but it doesn't work, it always return false, i don'tk now why, pls tell me whats wrong, thnx in advance!

    Code:
    /* Palindrome */
    
    #include <stdio.h>
    
    int testPalindrome( char array[] );
    
    int main()
    {
       char string[ 6 ] = "ellle";
    
       if ( testPalindrome( string ) == 1 )
          printf( "String is a palindrome.\n " );
       else if ( testPalindrome( string ) == 0 )
          printf( "String is not a palindrome.\n" );
    
       getch();
    
       return 0;
    }
    
    int testPalindrome( char array[] )
    {
       int length = 0;
       int i = 0;
    
       i = 0;
    
       while ( array[ i ] != '\0' ) {
          ++length;
          i++;
       }
    
       if ( length - 1 == 0 || length - 1 == 1 )
          return 1;
       else if ( array[ 0 ] != array[ length - 2 ] )
          return 0;
       else {
          array[ 0 ] = '\0';
          array[ length - 2 ] = '\0';
          for ( i = 0; i < 6; i++ )
             printf( "%c", array[ i ] );
    
          return testPalindrome( array );
       }
    }

  2. #2
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    You could write it

    Code:
    int palindrome(char* s)
    {
         return palindrome_recur(s, 0, strlen(s)-1);
    }
    
    int palindrome_recur(char* s, size_t low, size_t high)
    {
         if (low >= high) 
                 return 1;
     
         return s[low] == s[high] && palindrome_recur(s, low+1, high-1);
    }

  3. #3
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    is there a way of doin it without using pointers ?

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    use strrev() to reverse the string and then test with strcmp() to see if the reversed string matches the original.

    Should be about three lines.

    EDIT
    When I looked up strrev() in MSDN it has the code to do test for palindromes.
    Code:
    void main( void )
    {
       char string[100];
       int result;
    
       printf( "Input a string and I will tell you if it is a palindrome:\n" );
       gets( string );
    
       /* Reverse string and compare (ignore case): */
       result = _stricmp( string, _strrev( _strdup( string ) ) );
       if( result == 0 )
          printf( "The string \"%s\" is a palindrome\n\n", string );
       else
          printf( "The string \"%s\" is not a palindrome\n\n", string );
    }
    Last edited by novacain; 01-11-2002 at 12:30 AM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    the thing is, i am doin a exercise, which i can only use normal coding techniques. I can't use pointers coz i haven't learnt that yet and can't use any other functions except the stdio.h and the functoins that i wrote.

    thnx

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    OK, should have realised, no one needs to find palindromes on their own.
    Need a pointer to send the string into the function as an array and sending in the first element of the array (eg szBuffer[0]).
    Could do it all in the main function or make the string global to avoid passing the pointer.


    Code:
    int FindPalindrome(char   *szBuffer)
    {
    int    iStart=0,iEnd=0;
    
    while(szBuffer[iEnd] != '\0')
        iEnd++;
    //back off one so szBuffer[iEnd] is not = '\0'
    iEnd--;
    while(iStart<iEnd)
    {
        if(iStart==iEnd)//meet in the middle
            return TRUE;
        if( (szBuffer[iStart]) == (szBuffer[iEnd]) )
        {
             iStart++;
             iEnd--;
        }
        else return FALSE;//not matched so not a palindrome
    }
    return TRUE;
    }
    call this with


    if( FindPalindrome(szStringToTest) )
    {
    // is palindrome
    }
    else //is not
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  7. #7
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    do u mean that it's not possible to do it recursively without the use of pointers ?

    BTW your code worked beautifully thnx

  8. #8
    Unregistered
    Guest
    It's possible, just replace
    int FindPalindrome(char *szBuffer)
    with
    int FindPalindrome(char szBuffer[])

    When you use array notation to pass a string to a function the compiler will turn it into a pointer, so both of the above calls will do the same thing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Integer Emulation
    By Elysia in forum C++ Programming
    Replies: 31
    Last Post: 03-18-2008, 01:03 PM
  2. undefined reference
    By 3saul in forum Linux Programming
    Replies: 12
    Last Post: 08-23-2006, 05:28 PM
  3. C++ Operator Overloading help
    By Bartosz in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2005, 12:55 PM
  4. MSVC Template Constructor/Assignment Errors
    By LuckY in forum Windows Programming
    Replies: 3
    Last Post: 07-22-2005, 02:57 PM

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