recurtion example

This is a discussion on recurtion example within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #include <string.h> #define SIZE 20 int reverse (char s[], int size); int main(void) { char ...

  1. #1
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    recurtion example

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define SIZE 20
    
    int reverse (char s[], int size);
    
    int main(void)
    {
     char ch[SIZE];
    
     printf("Enter word: " );
     fgets(ch,SIZE,stdin);
    
     reverse(ch, strlen(ch)-1);
    
          system("PAUSE");
          return 0;
    }
    int reverse (char s[], int size )
    {
     if ( size == 0 )
        return 0;
     else{
          reverse( &s[0], size - 1 );
          printf("%c", s[size]);
     return 0;
     }
    }
    Okay this is what my do take in a word and print it in reverse order ...use recursive ... the question is??? lol Ah well i isnt printing it reverse why...Well i am not good with recursive functions trying to get a good grasp ...

    Thanks alot
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    *Game show music plays*

    Contestant 1 "Bob, I can write that function in eight lines!"
    Contestant 2 "I can write that function in seven lines!"
    Contestant 1 "I can write it in six lines!!"
    Contestant 2 "I can do that in five lines!"
    Contestant 1 "Write that code!"
    Code:
    void rev( const char * s )
    {
        if( s != NULL ) rev( s + 1 );
        printf("%c", s );
    }
    Quzah: "I can write it in ONE LINE!"
    Code:
    void rev( const char * s ){if(s!=NULL) rev(s+1);printf("%c",s );}
    Contestants 1 & 2 "Hey, that's the same code just all smashed up!"

    Quzah "Sure, but technicly, it is one line."


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    LOL seems ur happy lol Thanks alot
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

Popular pages Recent additions subscribe to a feed

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