A way to reverse a string using recursion in C str

2019-09-09 06:38发布

问题:

I found the following code to reverse a string in C in my university notes. I do not understand how the recursion works in this example since it seems magic to me! To be more specific, if I type the word "one" the program prints out the word "eno". According to the explanation, the function reads characters from the user recursively until the user presses Enter('\n') and then prints out the reversed word. But how is it possible to print the word if the last time the function calls itself recursively is when the user presses Enter and after that the program can't get into the function again in order to call the printf? Does it use some kind of buffer and how it works?? Here is the C source code:

#include <stdio.h>

void readCharsAndReverse(void);

int main (void)
{
    printf ("Give characters to reverse:");
    readCharsAndReverse();
    printf ("\n\n");
    system("PAUSE");
}

void readCharsAndReverse(void)
{
    char ch;
    scanf ("%c", &ch);
    if (ch != '\n')
       readCharsAndReverse();
    printf ("%c", ch);
 }  

回答1:

As soon as you enter Enter, your method readCharsAndReverse starts returning ie., first it will print the last entered character and all other recursive calls on stack start being poped off..thus by printing all the characters one by one in reverse order..

Its just like you keep something in bottom shelf and move onto upper shelf to do the same thing..and when you encounter Enter you paste whatever you have, on the wall and come back to below shelf and paste whatever you have kept there, on the wall next to previously pasted content, so on..



回答2:

Right, I think this is the answer you're looking for.

Your program has what is called a stack. Every time you call a function, space is created on this stack (a stack frame) to hold information regarding the function. This is typically any registers that are being modified (don't worry about this), the return address, or rather, then function that called it, and any variables used by the function.

Every time you call readCharsAndReverse, a new frame is placed onto the stack. This contains the local variable ch. When readCharsAndReverse exits, the current stack frame is removed (popped), the computer starts executing from where readCharsAndReverse was called (n your example, typically on the readCharsAndReverse() line). When this happens, the value of ch is maintained to be exactly as it was before you called readCharsAndReverse again.



回答3:

Basically, the function reads a character into its local variable ch, calls itself and prints ch.

That means something like;

  • readCharsAndReverse called first time reads o into ch and calls itself.

  • readCharsAndReverse called second time reads n into ch and calls itself.

  • readCharsAndReverse called third time reads e into ch and calls itself.

  • readCharsAndReverse called fourth time reads \n and returns to readCharsAndReverse called third time.

  • readCharsAndReverse called third time prints its ch, e and returns to readCharsAndReverse called second time.

  • readCharsAndReverse called second time prints its ch, n and returns to readCharsAndReverse called first time.

  • readCharsAndReverse called first time prints its ch, o and we're done.



回答4:

It worked, for example when you input "one" and press Enter: when you type o, the readCharsAndReverse() function is called the first time and store the first ch value to 'o', and because 'o' != \n so the readCharsAndReverse() called again, to the \n character, the function now look like :

readCharsAndReverse(){
     ch = 'o';
       readreadCharsAndReverse(){
         ch = 'n';
             readreadCharsAndReverse(){
                 ch='e';
                  readreadCharsAndReverse(){
                      ch='\n';

so because it is '\n' character, the print function run and will print from 'e' to 'o' -> result is th reserve string



回答5:

readCharsAndReverse is not stopped till "\n" is met, so in case of "one": "o" is scanned. It's not a char, so It continues to next one, which is "n", and after "e" we get "\n". Now we go to next line (since we came to our base case, so next line tells to print what we have in stack ( which is "eno". "e" first, "n" second, and "o" lastly).

I reccomand rading more, and seeing more examples, while writing the stack at any moments.



回答6:

Generally recursion works using stack. When a function is called it is pushed into the stack and this is repeated until a base condition is reached. At last when you reach the base condition each function is popped out. And then the printf statement gets executed.