TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: Sam Benson
from: Frank Adam
date: 1996-05-31 18:28:00
subject: Understanding recursion

G'Day Sam,
 
-=> Quoting Sam Benson to All <=-

 SB> void reverse (char line_in[], int index)
 SB> {
 SB> if (line_in[index])   
 SB> {
 SB> printf ("%c", line_in[index]);
 SB> index++;                       
 SB> reverse (line_in, index); 
 SB> }
 SB> printf ("%c", line_in[index]);
 SB> }
I think your problem is that by the time you reach the end of the first 
instance of the function index = 1.
So, here is a revised version..

void reverse (char line_in[], int index)
{
 if (line_in[index++])   /* increment index here */
 {
  printf ("%c", line_in[index]);  
  reverse (line_in, index);       
  }
 printf ("%c", line_in[--index]); /* decrement it here so we're still */
}                                 /* looking at the same char in this 
                                     instance*/                       

FWIW, recursion should not be used with this sort of thing, pointers will
make short work of the same task, while not relying on stack space, not 
to mention that the above function (recursive functions in general) is 
rather hard to follow.                                 
                                             
char* Rev_Str(char* str1,char *str2)
{
 char *ptr1 = str1,*ptr2 = str2;
printf("\n%s",str1); 
 while(*++ptr1);
 while(ptr1 != str1) *ptr2++ = *--ptr1;
 *ptr2 = '\0';
printf("\n%s",str2);
 return str2;
 }   
  

  L8r Frank (fadam{at}ozemail.com.au).
                
___ Blue Wave/DOS v2.21
     

--- Maximus 3.01
* Origin: The Software Parlour (3:635/544)
SEEN-BY: 50/99 620/243 623/630 632/349 635/503 544 727 711/401 409 410 413
SEEN-BY: 711/430 808 809 932 934 712/515 713/888 714/906 800/1
@PATH: 635/544 50/99 711/808 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.