用递归实现字符数组的反转

用递归实现字符数组的反转

比如 char a[10] = {''a', 'b', 'c'};

用递归实现后打印为 'c’, 'b', 'a'

代码实现:

#include<stdio.h>
#include<string.h>

void reverse(int length, char a[]);

int main(){
    char a[10] = {'a', 'b', 'c', 'd', 'e'};
    int length = strlen(a);
    reverse(length, a);
}
void reverse(int length, char a[]) {
    if (length == 0) {
        return;
    }
    int index =  length - 1;
    printf("%c\n", a[index]);
    reverse(index, a);
}

运行结果:

e
d
c
b
a
(0)

相关推荐