![](/img/trans.png)
[英]How to reverse a array of chars and then store the reversed array of char to another array?
[英]How to make a reversed array from another array in C
我想為 C 中的另一個數組創建一個反轉數組。
例如:
int nums[6]={1,2,3,4,5,6};
我希望“反轉”是數組標稱的反轉數組
int reverse[6]={6,5,4,3,2,1};
到目前為止我所擁有的:
#include <stdio.h>
#include <string.h>
int main(){
int nums[6]={1,2,3,4,5,6};
int size=sizeof(nums)/sizeof(nums[0]);
int reversed[size];
for (int i=size;i>0;i--){
reversed[size-i]=nums[i];
}
return 0;
}
一個非常簡單的 function 反轉數組將是:
void reverse(int array[], int start, int end)
{
int tmp;
while (start < end)
{
tmp = array[start];
array[start] = array[end];
array[end] = tmp;
start++;
end--;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.