> --- David <[EMAIL PROTECTED]> wrote:
A simple iterative one could be (IMHO far more
> > efficient else there is a 
> > bug)
> > 
> > char* reverse( char* str ) {
> >     char* begin = str, *end = str;
> >     while( *end ) ++end;    // go to the end
> >     while( end > begin ) {  // swap begin and end
> > until they join
> >         char c = *--end;
> >         *end = *begin;
> >         *begin++ = c;
> >     }
> >     return str;
> > }
> > 
--- Mickey Mathieson <[EMAIL PROTECTED]> wrote:
Below is a more efficient and much clear code.
Mickey



char *RevStr(char *str)
{
        int len = strlen(str);
        for (int i = 0; i < len/2; i++)
        {
         char C = str[i];
         str[i] = str[len-i-1];
         str[len-i-1] = C;
         }
        return(str);
}



> 

> 
> > Mickey Mathieson a écrit :
> > > I am new to the group and have noticed a lot of
> > questions of 
> > > recursion.
> > > A problem that is suitable for recursion should
> > produce a smaller 
> > > function that if the problem was solved without
> > recursion. This has
> > > not been the case from the postings that i have
> > looked at. 
> > >
> > > I have provided an example of recursion done
> > correctly to reverse a 
> > > string. Notice how short and compact the code
> is.
> > This is the object
> > > of recursion - less code at the expense of more
> > memory being used.
> > >
> > > Mickey M.
> > > senior software engineer
> > > http://www.constructionpartner.com
> > 
> // Reverse String
> char *RevStr(int count, char *str)
> {
>       if (!count) return(str);
>       char Value = str[count-1];
>       RevStr(count-1, str);
>       str[strlen(str) - count] = Value;
>       return(str);
> }
> 
> int main(int argc, char* argv[])
> {
> 
>       char Test[20] = "ABCDEFGHIJK";
>       char Out[20];
> 
>       strcpy(Out,RevStr(strlen(Test), Test));
> 
> 
>       return 0;
> }
> 
> 
> The problem is about Recursion - not using loops.
> Sure I could shorten it more but why to confuse the
> people i am trying to explain it too!
> 
> Anyone can come up with a loop solution such as you
> did. Again this did not answer the question.
> 
> Mickey
> 
> 
> 
> 
> 
>  
>
____________________________________________________________________________________
> Never miss an email again!
> Yahoo! Toolbar alerts you the instant new Mail
> arrives.
> http://tools.search.yahoo.com/toolbar/features/mail/
> 



 
____________________________________________________________________________________
Bored stiff? Loosen up... 
Download and play hundreds of games for free on Yahoo! Games.
http://games.yahoo.com/games/front

Reply via email to