Use the recursive method to output the string in reverse order

Recursive thinking to achieve reverse order output:
call the function repeatedly until the last character is found, and then output the previous character of the character layer by layer, and the reverse order effect is formed on the output~

#include <stdio.h>

void Reverse(char* s) {
    
    
	int len = strlen(s);
	if (len == 1)
	{
    
    
		printf("%c", *s);
	}
	else
	{
    
    
		Reverse(s + 1); //若不为最后一个字符,则递归调用Reverse函数
		printf("%c", *s);//当调用结束时逐层输出字符,形成逆序效果~
	}
}

int main() {
    
    
	char s[100];
	printf("请输入字符串:\n");
	scanf("%s", s);
	Reverse(s);
	return 0;
}

Guess you like

Origin blog.csdn.net/Genius_bin/article/details/112547504