help to understand code recursive string


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>
#define SIZE 80
void reverse(const char * const sPtr);
int main(void)
{
char sentence[SIZE]; // create char array
puts("Enter a line of text:");
fgets(sentence, SIZE, stdin);
puts("\nThe line printed backward is:");
reverse(sentence);
     }
  // recursively outputs characters in string in reverse order
 void reverse(const char * const sPtr)
 {
// if end of the string
if ('\0' == sPtr[0]) { // base case
    return;
} // end if
else { // if not end of the string

       // use fgets to read line of text

    reverse(&sPtr[1]); // recursion step
    putchar(sPtr[0]); // use putchar to display character
}
  }

	



The program calls recursive function reverse to print the line of text backward. i understand that cause reverse to walk until terminating null char, but then after that it return to last call which is reverse in reverse function. immediately then execute putchar [sPtr=0], sPtr=0 here is last word/null/'\n'?
what makes putchar can display word from the sPtr[0] reverse, though no increment?

for example i have "abc\n\0" then the pointer will walk until '\0' if '\0' then return to last function call which is execute putchar what makes it can execute putchar statement untill all words are displayed though there is no calling function? what makes putchar can display word recursive when there is no increment or decrement?

and also i was confuse with debugging, it show only where the pointer points at as [0]? actually i read "abc\n\0" as sPtr[0],[1],[2],[3],[4] while debug always shows where is the pointer point at, here after last recursive, sPtr[0] is \n then how can it display recursive? moving to left(?)

Here is the code with consistent indentation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
#define SIZE 80
void reverse(const char *const sPtr);
int
main(void)
{
    char sentence[SIZE];        // create char array
    puts("Enter a line of text:");
    fgets(sentence, SIZE, stdin);
    puts("\nThe line printed backward is:");
    reverse(sentence);
}

// recursively outputs characters in string in reverse order
void
reverse(const char *const sPtr)
{
    if ('\0' == sPtr[0]) {      // base case
        return;
    } else {
        reverse(&sPtr[1]);      // recursion step
        putchar(sPtr[0]);       // use putchar to display character
    }
}

Take the case where you pass "abc\n" into reverse.
- In the first call, sPtr is "abc\n".
- At line 21, reverse() calls itself recursively, passing "bc\n" as sPtr. Remember that arrays in C++ start at index 0, so the call reverse(& sPtr[1]) is passing the address of the second character in sPtr, not the first.
- The second call makes another recursive call, passing "c\n"
- The second makes a third, passing "\n" and the third makes a fourth, passing "".

Now keep in mind that each call has its own copy of sPtr. The first call's sPtr pointer is a different variable from the second call's sPtr and the second call's sPtr is different from the third etc.

Okay, we were starting the fourth call, where sPtr == "". This triggers the base case, so the fourth call just returns to the third call.
- The third call continues at line 22 and prints sPtr[0]. The third call's sPtr points to "\n", so that's what it prints.
- The third call returns, causing the second call to continue at line 22. The second call's sPtr points to "c\n" so it prints 'c' and returns.
- That causes the second call to continue. It prints 'b' and returns
- That causes the first call to contine. It prints 'a' and returns.
@dhayden Thankou for explanation in detail!!!!!'
Return here means back to last call which is the third call?
And then after it , it execute putchar sPtr[0] which is \n
But what makes it comeback again to reverse(&sPtr[1]) in main after it executed putchar?
What makes it back to second call to continue at line 22?
Although it finish execute putchar?

Abc\n\0
<-0
What makes it goes print backward?
Debug always shows where pointer point as 0, i was confuse what word exist after zero and behind zero(?)
Can you explain abit in debug perspective? Like abc\n\0 until 0 then
Why it print backward and not continue to right(?)
It may be easier to understand by changing the output of reverse() to display what it's doing:
1
2
3
4
5
6
7
8
9
10
11
12
void
reverse(const char *const sPtr)
{
    printf("In reverse(%s)\n", sPtr);
    if ('\0' == sPtr[0]) {			 // base case
	return;
    } else {
	reverse(&sPtr[1]);			 // recursion step
	printf("reverse(%s) prints %c\n", sPtr, sPtr[0]);
	// putchar(sPtr[0]);			 // use putchar to display character
    }
}

Enter a line of text:
abc

The line printed backward is:
In reverse(abc             <-----
)                          <----- This is printing "abc\n"
In reverse(bc
)
In reverse(c
)
In reverse(
)
In reverse()
reverse(
) prints

reverse(c
) prints c
reverse(bc
) prints b
reverse(abc
) prints a

Topic archived. No new replies allowed.