Thread: Recursion: base case returning 1, function returning 0

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    182

    Recursion: base case returning 1, function returning 0

    Hey,
    I'm writing a recrusive function that is supposed to test a palindrome. I'm testing it out by inputing "racecar". My test statements indicate to me that the function should be returning 1(meaning it is a palindrome). However I'm getting returned 0. My understanding of recursion is a bit lacking right now I have a feeling I'm not fully understanding how a parent function handles the returned value. Could someone help me please?

    Code:
    #include <stdio.h>
    
    int main()
    {
       char string[ 15 ];
       scanf("%s", &string);
    
       printf("%d\n", testPalindrome(string, 6, 7) );
    
    return 0;
    }
    
    
    
    int testPalindrome(char array[], int n, int size)
    {
       if (size % 2 == 1 && (size - 1) / 2 == n ) {
          // TEST STATEMENT
          printf("if 1\n");
          return 1;
       }
       else if (size % 2 == 0 && (size - 1) / 2 == n) {
          // TEST STATEMENT
          printf("if 2\n");
          if (array[ n ] == array[ size - n ])
             return 1;
          else
             return 0;
       }
       else {
          // TEST STATEMENT
          printf("if 3\n");
          if (array[ n ] == array[ size - n - 1 ])
             testPalindrome(array, n - 1, size);
          else
             return 0;
       }
    
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    where's your function declaration?

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You may want to add a return to the call to testPalindrome() inside testPalindrom(). With that, it works on my machine. With warnings, the compiler says "not all paths have a return" (or some such) without the return, so you may want to enable warnings in the compiler.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    182
    thanks for the observation

    i figured out i didn't specify what value to return. its working now

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    182
    Quote Originally Posted by matsp View Post
    You may want to add a return to the call to testPalindrome() inside testPalindrom(). With that, it works on my machine. With warnings, the compiler says "not all paths have a return" (or some such) without the return, so you may want to enable warnings in the compiler.

    --
    Mats

    thank you

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I simplified your code a bit.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int testPalindrome(char array[], int n, int size);
    
    int main()
    {
       char string[ 15 ];
       scanf("%s", string);
    
       printf("%d\n", testPalindrome(string, strlen(string)-1, strlen(string)) );
    
    return 0;
    }
    
    
    
    int testPalindrome(char array[], int n, int size)
    {
       if (size % 2 == 1 && (size - 1) / 2 == n ) {
          // TEST STATEMENT
          printf("if 1\n");
          return 1;
       }
       else {
          // TEST STATEMENT
          printf("if 3\n");
          if (array[ n ] == array[ size - n - 1 ])
             return testPalindrome(array, n - 1, size);
          else
             return 0;
       }
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM