Thread: How do I modify my code to add recursive and non-rescursive function..

  1. #1
    Banned
    Join Date
    Apr 2011
    Posts
    56

    How do I modify my code to add recursive and non-rescursive function..

    Hello everyone,

    I had been working on this project recent days and finally got it work. The problem is I couldn't do it the way question asked me so, I did it my way. The problem is regarding Palindrome. I check if the word or sentence is a palindrome and the shows you the output. You can check the code to see how it works.
    My question is how can I modify it so that it will have 2 functions. 1 non-recursive function to determine if a string is palindrome or not. Add another function (recursive) doing the same thing. I tried placing them in between my program but it never worked. Can someone help me with that?
    Here is my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    #define MAX_STR_LEN 80
    typedef enum { false = 0, true } bool;
    
    void erase(char *, size_t);
    bool isPalindrome(const char *, const char *);
    char in[MAX_STR_LEN];
    
    int main(int argc, char *argv[]) {
        size_t x;
    
        while (1) {
            printf("> ");
            fgets(in,MAX_STR_LEN,stdin);
    
            /*
              * Remove trailing newline from entered string, then
              * remove all spaces and punctuation from string, and
              * convert all to lower case.
              */
            *(strchr(in,'\n')) = '\0';
            for (x = 0; x < strlen(in); ) {
                in[x] = tolower((int)in[x]);
                if (isspace((int)in[x]) || ispunct((int)in[x])) {
                    erase(in, x);
                } else {
                    x++;
                }
            }
    
            /*
              * Check if the result is a palindrome.
              */
            printf("\"%s\" is ",in);
            if (isPalindrome(in, in + strlen(in) - 1) == false) {
                printf("not ");
            }
            printf("a palindrome.\n\n");
        }
        return 0;
    }
    
    bool isPalindrome(const char *p, const char *q) {
        if (q < p) return true;
        if (*q != *p) return false;
        return isPalindrome(p + 1, q - 1);
    }
    
    void erase(char *s, size_t x) {
        memmove(&s[x], &s[x+1], strlen(s) - x);
    }
    Thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'm not entirely certain what your question is about. I would suggest showing the failed version, as then we can see what issue you are having (the "working" version would obviously not have any issues, right?).

  3. #3
    Banned
    Join Date
    Apr 2011
    Posts
    56
    All needs to be done is to use functions and do the same thing with both but 1 will work using recursion and other without recursion. It will be something like this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    #define MAX_STR_LEN 80
    typedef enum { false = 0, true } bool;
    
    void erase(char *, size_t);
    bool isPalindrome(const char *, const char *);
    char in[MAX_STR_LEN];
    void recurse(int);
    
    int main(int argc, char *argv[]) {
        size_t x;
    
        while (1) {
            printf("> ");
            fgets(in,MAX_STR_LEN,stdin);
    
            /*
              * Remove trailing newline from entered string, then
              * remove all spaces and punctuation from string, and
              * convert all to lower case.
              */
            *(strchr(in,'\n')) = '\0';
            for (x = 0; x < strlen(in); ) {
                in[x] = tolower((int)in[x]);
                if (isspace((int)in[x]) || ispunct((int)in[x])) {
                    erase(in, x);
                } else {
                    x++;
                }
            }
    
            /*
              * Check if the result is a palindrome.
              */
        void recursive (int in) //  --  > use recursion here
            printf("\"%s\" is ",in);
            if (isPalindrome(in, in + strlen(in) - 1) == false) {
                printf("not ");
            }
            printf("a palindrome.\n\n");
        }
        return 0;
    }
    
    bool isPalindrome(const char *p, const char *q) {
        if (q < p) return true;
        if (*q != *p) return false;
        return isPalindrome(p + 1, q - 1);
    }
    
    void erase(char *s, size_t x) {
        memmove(&s[x], &s[x+1], strlen(s) - x);
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, but hang on. You said you were supposed to have a recursive function and a non-recursive function. You've already got the recursive function -- isPalindrome calls itself and is recursive. You don't need to try to somehow make it recursive, and you surely aren't required to write a function called "recursive".

    And I don't know how you could have gotten this far, and written the part above, without realizing that you can't insert functions inside other functions. You need to write another function (which will look like all the other functions you've written, in terms of the form), that is a non-recursive function to determine whether or not a string is a palindrome.

  5. #5
    Banned
    Join Date
    Apr 2011
    Posts
    56
    I know that was just the for example. But the problem is that question says to make 2 different functions which will do the same thing but one will do using recursion and other non-recursion. Thats where I am confused. I have done everything but not sure what the question is asking for. If I made two functions doing same thing then wouldn't they conflict?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Iron Hide View Post
    I know that was just the for example. But the problem is that question says to make 2 different functions which will do the same thing but one will do using recursion and other non-recursion. Thats where I am confused. I have done everything but not sure what the question is asking for. If I made two functions doing same thing then wouldn't they conflict?
    No? (You have to give them different names obviously, but that's not really a hardship.)

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You call each function in turn
    Code:
            /*
              * Check if the result is a palindrome.
              */
            printf("\"%s\" is ",in);
            if (isPalindrome(in, in + strlen(in) - 1) == false) {
                printf("not ");
            }
            printf("a palindrome.\n\n");
    
            /*
              * Check if the result is a palindrome (non-recursive)
              */
            printf("\"%s\" is ",in);
            if (isNonRecursivePalindrome(in, in + strlen(in) - 1) == false) {
                printf("not ");
            }
            printf("a palindrome.\n\n");
    Just write the same function again, but without using recursion (and with another name), and you're done.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Banned
    Join Date
    Apr 2011
    Posts
    56
    Will I use iteration (for loop) for non-recursive function?

    I can't use same code again. I will do ame thing using recursion and other will us iteration(for loop).

  9. #9
    Banned
    Join Date
    Apr 2011
    Posts
    56
    Does anyone still doesn't understand what this question is asking for?

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Sounds more like YOU don't understand.

    You need to write two completely separate functions that do the same thing. You have several options.
    1) Two separate files each with a main() that does your palindrome thing
    2) One file, one main(), and within main, call, sequentially, your two different versions of the palindrome thing.

    I'd opt for 2).

    Create two functions, name one function palindrome_recursive() and the other palindrome_non_recursive()

    Then in the main() call each one and display the results. The user might see something like this:

    Code:
    Enter your text: (here the user types hello)
    Recursive palindrome: olleh
    Non-recursive palindrome: olleh
    Last edited by mike65535; 05-11-2011 at 01:18 PM.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Iron Hide View Post
    Does anyone still doesn't understand what this question is asking for?
    It seems to me that you're the only one who doesn't understand what is being asked for, or what the difference between recursion and iteration are. The advice given so far is fine, you just need to follow it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675

  13. #13
    Banned
    Join Date
    Apr 2011
    Posts
    56
    ok rags you got any problem with that. I can post it where ever I want for fast response. If you can't answer than please don't pull your leg in my thread.

    Thanks

  14. #14
    Banned
    Join Date
    Apr 2011
    Posts
    56
    Thanks all, I figured that one out, but I also need a little in in do while loop
    What I am doing wrong here?

    Thanks

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Iron Hide View Post
    Thanks all, I figured that one out, but I also need a little in in do while loop
    What I am doing wrong here?

    Thanks
    And he's a spammer, too...

    Really ... same code, two threads and cross posting on top.

    Iron Hide... this is *not* the way to make friends and influence people...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Make Recursive function 'Tail-Recursive'
    By dp2452 in forum C Programming
    Replies: 7
    Last Post: 12-04-2009, 10:13 AM
  2. can someone help me modify the code of Metapad?
    By Rigel in forum C Programming
    Replies: 2
    Last Post: 10-17-2009, 09:17 AM
  3. modify my code
    By BEN10 in forum C Programming
    Replies: 3
    Last Post: 03-28-2009, 01:48 AM
  4. [code] Recursive Search Function
    By anonytmouse in forum Windows Programming
    Replies: 1
    Last Post: 12-17-2004, 11:21 PM
  5. Recursive Function in Pseudo Code
    By smitsky in forum Tech Board
    Replies: 3
    Last Post: 10-24-2004, 10:17 AM