Thread: palindrom using recursion?

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    67

    palindrom using recursion?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    int is_palindrom(char niz[]);
    
    int main(int argc, char *argv[])
    {
      if(argc<2)
        { 
          printf("Need an argument-a word!\n");
          exit(1);
        }
    
      if(strlen(argv[1])==0 || strlen(argv[1])==1)
        { printf("You have to input a word ,not a character!\n");
        exit(1);
        }
    
      if(is_palindrom(argv[1])==0)
        {
          printf("A word %s is palindrom!\n",argv[1]);
        }
    
      else printf("A word %s is not a palindrom!\n",argv[1]);
     
    }
    
    int is_palindrom(char niz[])
    {
      char reverse_niz[strlen(niz)];
      int i;
      int x=0;
      for(i=strlen(niz)-1;i>=0;i--)
        {
          reverse_niz[x]=niz[i];
          x++;
        }
      reverse_niz[x]='\0';
         
      if(strcmp(reverse_niz,niz)==0)
        return 0;
    
      else 
    
        return 1;
    
    }
    The above program works. A palindrom is a a word which can be read backwards the same as forward(ex. cepec). In the above program i first reversed the inputed word and stored it into reverse_niz and then compare that string to the original. And if the words are the same the thing is palindrom.

    BUT I have to implement that function is_palindrom(char niz) using recursion. Somehow I have to make for the method to be callig itself in the body of a method.

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Do you understand the concept of recursion?
    Code:
    int factorial(int n)
    {
      if( n < 2 ) return 1;
      return n * factorial(n-1);
    }
    There's a classical example of using recursion. As you can see, it's split into two parts. The first line, the base case, is essential. That line ensures that the factorial function ends eventually.

    For your program, the base case isn't difficult. If you have a string one or fewer characters long, it's vacuously true that it's a palindrome.

    The second part is a bit more difficult. Thinking recursively takes practice.

    For your program, this part of the function will do essentially the same thing as the body of your loop does: check if one char is equal to another char.

    Now you just need to put this algorithm into code, making sure that it doesn't end up calling itself endlessly.
    Last edited by joshdick; 04-27-2005 at 07:35 AM.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    67
    ...what a dounting task

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Then start with a simpler one. For instance, how about trying to write a recursive function to print the numbers 1 to n?
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    67
    so if n is 3 the output would be 6?

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    no if n is 3 it would print
    1 2 3

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    67
    Quote Originally Posted by joshdick
    Then start with a simpler one. For instance, how about trying to write a recursive function to print the numbers 1 to n?
    Code:
     int count (int n) {
     int i=1;
     int num=count(n-n+i);
          while(num<=n)
         {
        
        num=count(n-n+i);
       printf("%d\n",num);
          i++;
           }
       
     }
    Maybe like this? I will give it a try.
    Last edited by blackswan; 04-27-2005 at 08:08 AM.

  8. #8
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Did you notice how the factorial function I provided worked without incrementing a counter or using a while loop? You don't need those things in your recursive function.

    Also, (n-n+i) is just i.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  9. #9
    Registered User
    Join Date
    Apr 2005
    Posts
    67
    Code:
     int count (int n) {
        if(n>=1)
        return n - count(n-1);
        }
    Maybe like this?
    Any good tutorials about recursion,cause this is a bit new to me.

  10. #10
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Start here

    - Micko
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  11. #11
    Registered User
    Join Date
    Apr 2005
    Posts
    67
    thanks micko!

  12. #12
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    You're welcome, later when you learn a little more about recursion come back. I have interesting recursive solution of your problem but I'll wait and see how you will handle this.

    - Micko
    Last edited by Micko; 04-27-2005 at 09:01 AM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  13. #13
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    "I have discovered a truly marvelous demonstration of this proposition that this margin is too narrow to contain."

    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  14. #14
    Registered User
    Join Date
    Apr 2005
    Posts
    67
    I am on it. Ill be back!

    btw: Thantos, I meant the factorial function. The result is 6 if n =3.

  15. #15
    Registered User
    Join Date
    Apr 2005
    Posts
    67
    I get a segmentation fault error on this one. Im trying to do that count thing from 1 to n. It looks ok to me . What did I do wrong?


    Code:
    #include<stdio.h>
    int count(int n);
    int main()
    
    
     {
       int z=3;
       printf("%d\n", count(z));
    
       return 0;
     }
    
    int count(int n) {
      int i=1;
      while(i<=n) {
        return  count(n)-count(n-i);
        i++;
      }
      return 1;
     
    
    }

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, 09:45 PM
  2. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 12:00 AM
  3. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 10:56 AM
  4. selection sorting using going-down and going-up recursion
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 02:29 PM