a question about recursion

This is a discussion on a question about recursion within the C Programming forums, part of the General Programming Boards category; I wrote a recursive function to find if a given string is a palindrome Code: #include <stdio.h> int isPalindromeRec(char *start, ...

  1. #1
    Apprentice
    Join Date
    Oct 2006
    Location
    LA
    Posts
    53

    a question about recursion

    I wrote a recursive function to find if a given string is a palindrome

    Code:
    #include <stdio.h>
    
    int isPalindromeRec(char *start, char* end);
    
    int main()
    {
       char str[] = "abccba";
    
       //pointers to string
       char* start = str;
       char* end = str;
    
       while(*end != '\0')
    	end++;
    
       end--;
    
       if(isPalindromeRec(start, end))
    	printf("String is a palindrome");
    
       else
    	printf("Not a palindrome");
    
       getchar();
    
       return 0;
    }
    
    
    //recursive function to check if a string is a palindrome
    int isPalindromeRec(char *start, char* end)
    {
       if(end<=start)
    	return 1;
    	
      //check if the two characters match
       if(*start==*end)
       {
    	start++;
    	end--;
    	isPalindromeRec(start, end);
       }
       else
    	return 0;
    }
    This code works fine.

    I changed the isPalindromeRec function to

    Code:
    int isPalindromeRec(char *start, char* end)
    {
       if(end<=start)
    	return 1;
    	
       else 
       {
    	return(*start==*end && isPalindromeRec((start++), (end--)));
       }
    }
    which is the same as the function above. However, it doesnt work the same way. On debugging I noticed that the start and end pointers are not incremented inside the isPalindromeRec((start++), (end--)) call.

    Why does this happen?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,566
    The postfix version of ++ and -- happen after everything else. That means the current versions are passed into the recursive call, then the local copy is incremented. That means you continually call isPalindromeRec with the original values of start and end, you never pass in the next values for them. Try ++start and ++end instead.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,169
    You're using post-increment and post-decrement. You should be using pre-inc/dec instead (e.g. isPalindromeRec(++start, --end);

    EDIT: Beaten to the punch
    If you understand what you're doing, you're not learning anything.

  4. #4
    Apprentice
    Join Date
    Oct 2006
    Location
    LA
    Posts
    53
    Ok thanks this works. I assumed that since I had brackets around the post-incr and post-decr operators, it would happen before the recursive call.

    Thanks again anduril462 and itsme86

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,566
    Parentheses themselves only force that part of an expression to be evaluated before other, unparenthesized parts of the expression. Each parameter is it's own expression, so i++ and (i++) behave the same. The following links might explain a bit:
    Sequence point - Wikipedia, the free encyclopedia.
    Question 3.8

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 08:45 PM
  2. iteration and recursion question
    By Stonehambey in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2008, 06:16 PM
  3. simple recursion question
    By salvadoravi in forum C Programming
    Replies: 4
    Last Post: 12-30-2007, 06:53 AM
  4. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM

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