Thread: What is wrong with this program ?

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

    What is wrong with this program ?

    hi,

    after i ran this program it shows nothing, i have to use control + c to quit. I use debugger and it generate errors. How come, Pls tell me why thnx a lot.

    Code:
    #include <stdio.h>
    
    int palindrome( char string[] );
    
    int main()
    {
       char string[ 5 ] = "elle";
    
       if ( palindrome( string ) == 1 )
          printf( "It IS a palindrome." );
    
       getch();
       return 0;
    }
    
    int palindrome( char string[] )
    {
       int i = 0, length = 0;
    
       while ( string[ i ] != '\0' )
          ++length;
    
       if ( length - 1 == 1 || length - 1 == 0 )
          return 1;
       else if ( string[ 0 ] != string[ length - 2 ] )
          return 0;
       else {
          string[ length - 2 ] = '\0';
          return palindrome( &string[ 1 ] );
       }
    }

  2. #2
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    but each time 'i' is used to determine the 'changed' length. Coz each time it will cut the first element and the last element array so that the next pair can be compared. IF you're right, how do i fix it ?

    thnx

  3. #3
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Your code will generate an inifinte loop when calculating the length of the string as you are always testing the first element of the array. Try changing your pallindrome function to something like this -

    Code:
    int palindrome( char string[] )
    {
       int i = 0, length = 0;
    
       while ( string[ i++ ] != '\0' )
          ++length;
    
       if ( length - 1 == 1 || length - 1 == 0 )
          return 1;
       else if ( string[ 0 ] != string[ length - 1 ] )
          return 0;
       else {
          string[ length - 1 ] = '\0';
          return palindrome( &string[ 1 ] );
       }
    }

  4. #4
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    oh i get what u mean thnx

    but any idea why it still didn't test the string correctly ?

    thnx

  5. #5
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >>but any idea why it still didn't test the string correctly ?

    because you were comparing the first character with the last but one.

  6. #6
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    but thats what i am suppose to do, if it's a palindrome, the first and last character are the same.

  7. #7
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Yes, but not the first and second from last (last but one).

  8. #8
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    u mean 2nd last ?

    can you show the the correct way pls ?

    thnx in advance

  9. #9
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    hey all i finally got it, here's the solution, thnx for all help so far:

    Code:
    #include <stdio.h>
    
    #define SIZE 6
    
    int palindrome( char string[], int start, int end );
    
    int main()
    {
       char string[ SIZE ] = "radar";
    
       if ( palindrome( string, 0, SIZE - 2 ) == 1 )
          printf( "It IS a palindrome.\n" );
       else
          printf( "It is NOT a palindrome.\n" );
    
       getch();
       return 0;
    }
    
    int palindrome( char string[], int start, int end )
    {
       int length = 0;
       int i = 0;
    
       if ( start == end )
          return 1;
    
       while ( string[ i++ ] != '\0' )
          length++;
    
       if ( length - 1 == 1 || length - 1 == 0 )
          return 1;
       else if ( string[ start ] != string[ end ] )
          return 0;
       else
          return palindrome( string, start + 1, end - 1 );
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Maze Program - What am I doing wrong?
    By Gipionocheiyort in forum C++ Programming
    Replies: 20
    Last Post: 08-02-2007, 01:31 PM
  2. Replies: 5
    Last Post: 01-13-2007, 02:14 AM
  3. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 02:03 PM
  4. Whats wrong with this program - Output of a series
    By duffmckagan in forum C Programming
    Replies: 2
    Last Post: 07-26-2006, 09:57 AM
  5. Whats wrong with my program?
    By Ruflano in forum C++ Programming
    Replies: 5
    Last Post: 02-21-2002, 05:09 PM