Day 1 - Recursively output strings in reverse order using C language

16629960:

Topic description:

Enter a continuous string on the keyboard until you press Enter, and the screen will display the string in reverse order. For example, if you enter: Iam man!, you will get the output:! nam maI.

Topic analysis:

The idea of ​​recursion is used in the question, so consider creating a sub-function responsible for recursion, and then output the previously input string in reverse order.

code show as below:

#include <stdio.h>

void reverse_output () ;
int main ()
{
    printf ( "请输入字符串,以回车结束 :\n" );

    reverse_output ();

    printf ( "\n以上是反序的输出 " );

    return 0;
}

void reverse_output ( )
{
    char ch_input;
    ch_input=getchar(  );

    if ( ch_input != '\n' )
        reverse_output () ;
    else
        return ;

    printf ( "%c" , ch_input );
}

Output example:

 

Understanding of sub-functions:

Brief summary: The character entered first will not execute the printf() function because it needs to enter the next level of recursion. The printf() function will not be executed until the recursion returns to this level, thereby printing out the previously stored characters.

First define the variable ch_input to store the character variable obtained by the getchar() function

Then use if to determine whether the variable is "carriage return (\n)". If not, then enter the next level of recursion and continue to enter the next character; if it is, use the return statement to indicate that a carriage return character has been encountered, and the recursion will exit this level of return. Go to the upper level;

Only when the if judgment fails and the if statement ends, the following printf() function will be executed. At this time, the execution is at the innermost level of the recursion. The innermost printf() function is executed first, and then pushed to the upper level to execute printf again. () function to achieve reverse order output.

By returning to the previous layer and executing the printf() function, reverse order output is achieved.

law:

If the printf() function is placed before the recursive call, the output will be in positive order.

If the printf() function is placed after the recursive call, the output will be in reverse order.

Recursive understanding:

Recursion is actually a step in which a function calls itself to repeatedly implement its own function.

Calling itself is actually the process of one function calling another function, but the other function here turns out to be myself.

In recursion, it can actually be seen as going in layer by layer.

 The big square on the left is regarded as a function, and the function is named A. After executing the instructions sequentially, the small square in the middle calls function A again, so the code of function A is executed from the beginning in the next layer. Function A itself is not called in the next layer, but fish block B is executed. After the execution is completed It will continue to execute the remaining code of the current layer. After completion, it will return to the location where A was just called in the first layer, and then continue execution.

Based on the above ideas, we can find that this is not an ordinary function calling idea.

Function call: A calls B, B calls C, C calls D, D returns to C, C returns to B, and B returns to A.

So if you use the idea of ​​​​function calling to write a recursive program

Note: The limitation of writing with functions is that each layer must write a separate function, so I only write three, which means that only three layers of recursion are executed.

Code example:

#include <stdio.h>
void A();
void B();
void C();

//主函数中去调用A函数
int main ()
{

    printf ( "请输入字符串,以回车结束 :\n" );
    A();

    return 0;
}

void A ()
{
    int ch=getchar();
    B();                    //调用下一层函数
    printf ( "%c" , ch );
    return ;
}

void B ()
{
    int ch=getchar();
    C();                    //调用下一层函数
    printf ( "%c" , ch );
    return ;
}

void C()
{
    int ch=getchar();
    printf ( "%c" , ch );        //没有继续调用,所以开始返回上一层
    return ;
}

Output example:

In addition, because there are only three sub-functions, if the number of input characters exceeds 3, output loss will occur.

Output example 

Replenish:

Using the recursive idea, you can implement the first-in-first-out FIFO of the queue and the first-in-last-out FILO of the stack in the data structure.

Article reference:

C language daily practice - Day 18: Use recursive function calling to print out the entered characters in reverse order_Xiaohui_Super's blog-CSDN blog icon-default.png?t=M3K6https://blog.csdn.net/weixin_43772810/ article/details/120464271

Understanding the nature of recursion: recursion and stack_bobbypapa's blog-CSDN blog_recursive stack icon-default.png?t=M3K6https://blog.csdn.net/bobbypollo/article/details/79891556

 

Guess you like

Origin blog.csdn.net/qq_43323677/article/details/124346649