Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Exercise 27

C Exercise 27

100 Classic C Language Examples 100 Classic C Language Examples

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

**Analysis:** None.

Example

// Created by www.runoob.com on 15/11/9.

// Copyright (c) 2015 Runoob Tutorial. 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 Language Examples 100 Classic C Language Examples