0%

将一个数组逆序输出

文章时效性提示

本文发布于 496 天前,部分信息可能已经改变,请注意甄别。

题目:将一个数组逆序输出。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
void Reverse(char* ch,int sz)
{
    int i,j=0;
    char temp[sz];
    for (i=sz-1; i>=0; i--)//先把逆序的数组保存到一个新的临时数组中
    {
        temp[j] = ch[i];
        j++;
    }
    for (i=0; i<sz; i++) //把新的临时数组内容赋值给原数组
    {
        ch[i] = temp[i];
    }
}
int main() {
    char ch[] = "abcdefghijsdfwdfasdfewfdqxsaefw";
    int sz = sizeof(ch)/sizeof(ch[0])-1;
    Reverse(ch,sz);
    printf("%s",ch);
    return 0;
}