Thread: Feedback on recursive function

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    49

    Feedback on recursive function

    The recursive function is bolded, i got feedback and was told that the static variable made the function seem a lot like a iterative function but he did not say why. Can someone explain it to me?

    Code:
    #define MAX 100
    #include <string.h>
    #include <stdio.h>
    
    
      int checkPalindrome(char string[MAX]);
      int checkRecPalindrome(char string[MAX]);
    
    
      int main(void){
        char checkString[MAX], chr[5] = {"exit"};
        do{
          printf("input string to check if it is a palindrome, or input exit to end\n");
          fflush(stdout);
          gets(checkString);
          if(strcmp(checkString, chr) != 0){
            /*call copies and reverses string to copy and sends back result of strcmp*/
            if(checkPalindrome(checkString) == 1)
              printf("The word is a palindrome(iterative)\n");
            else
              printf("The word is not a palindrome(iterative)\n");
            if (checkRecPalindrome(checkString))
              printf("The word is a palindrome (recursive)\n");
            else
              printf("The word is not a palindrome(recursive)\n");
          }
          else
            printf("Quitting...");
           /*loop is continued until checkString matches chr with strcmp*/
        }while(strcmp(checkString, chr) != 0);
        return 0;
      }
      int checkPalindrome(char string[MAX]){             /*strrev could also be used for a shorter code*/
        char revString[MAX];
        int a, b = (strlen(string) - 1) ;
        for(a = 0; a < strlen(string); ++a){
          revString[b] = string[a];
          --b;
        }
        revString[strlen(string)] = '\0';
        if(strcmp(string, revString) == 0)
          return 1;
        else
          return 0;
      }
      int checkRecPalindrome(char string[MAX]){
        static int a = 0;                               
        int strLgt = strlen(string) - a;                 
        if(strLgt > a){                                  
          if(string[a] == string[strLgt - 1]){
            a++;
            return checkRecPalindrome(string);
          }
          else
            return 0;
        }
        a = 0;
        return 1;
      }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There are situations where a function needs to keep a static state; this is not one of them. That value would normally be passed as one of the parameters to the function. (Actually you would probably pass the "length" of the string -- the original caller would pass the actual length, and then the recursive function calls would knock two off each time.)

    I don't know exactly how that makes it seem "iterative" though.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Brolaf Broski
    i got feedback and was told that the static variable made the function seem a lot like a iterative function but he did not say why. Can someone explain it to me?
    Then ask the person who gave you feedback. I can guess that it feels like you're using a loop index, but the person may have a different reason.

    Personally, I avoid static local variables with recursion because I find that it makes it harder to reason about the recursion since you have to consider state that persists from call to call.

    Are you permitted to add a parameter to checkRecPalindrome? Are you permitted to modify the string, or at least make a copy of it and then do the recursion in another function with the copy? I ask this because given just one parameter (pointer to the first element of the string) seems insufficient for a recursive solution without the use of a static local variable (or global variable, for that matter), if the string is not to be modified and no copy can be made.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Nov 2013
    Posts
    49
    the assignment said to create the recursive function with 1 parameter only.

  5. #5
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    /*strrev could also be used for a shorter code*/
    How about completely rewriting it?
    Code:
    static int pali_(const char *start, const char *end)
    {
    	return start >= end ? 1 : *start == *end && pali_(start + 1, end - 1);
    }
    
    int pali(const char *s)
    {
    	return *s != '\0' && pali_(s, s + strlen(s) - 1);
    }
    i dont believe in competition in da field of cboard posts, u see, i believe in a collection of posts each 2 be admired for der own personal statement. when in doubt, ask Willy!

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Brolaf Broski
    the assignment said to create the recursive function with 1 parameter only.
    Right. Are you then permitted to modify the string? I presume that having a recursive helper function won't do (as in Barney McGrew's example) since your instructor would then say that the recursive function has more than one parameter.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    You can do it with one parameter and the return value

  8. #8
    Registered User
    Join Date
    Nov 2013
    Posts
    49
    actually he said that we could use a helper function I had no idea what he meant. I will take a look at Barney's functions. This forum is great for a beginner!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-18-2013, 11:01 PM
  2. Converting recursive function to tail recursive
    By ajacobs365 in forum C Programming
    Replies: 1
    Last Post: 10-30-2011, 08:15 AM
  3. Replies: 1
    Last Post: 12-03-2010, 01:54 AM
  4. Make Recursive function 'Tail-Recursive'
    By dp2452 in forum C Programming
    Replies: 7
    Last Post: 12-04-2009, 10:13 AM
  5. Recursive function
    By Fork in forum C Programming
    Replies: 3
    Last Post: 10-26-2006, 11:27 AM