C Exercise Example 27

100 Classic C Examples

Title:Use recursive function calls to print the 5 input characters in reverse order.

Program analysis:None.

Example

// Created by www.example.com on 15/11/9. // Copyright © 2015 Example. All rights reserved. // #include <stdio.h> int main() { int i=5; void palin(int n); printf("Please enter 5 characters\40:\40"); palin(i); printf("\n"); } void palin(n) int n; { char next; if(n<=1) { next=getchar(); printf("Output the result in reverse order\40:\40"); putchar(next); } else { next=getchar(); palin(n-1); putchar(next); } }

The output of the above example is:

请输入5个字符 : abcde
相反顺序输出结果 : edcba

100 Classic C Examples

other extensions