Reverse an Array in C
To reverse an array in C, we swap its elements from the beginning with those at the end until we reach the middle of the array. This can be achieved using loops such as for
or while
, or by storing elements in a temporary array and printing them in reverse order.
In this tutorial, we will cover how to reverse an array using temporary array, or swapping elements in the array, or using recursion.
Examples to Reverse an Array
1. Reverse an Array Using a Temporary Array
In this example, we will create a new temporary array and store elements in reverse order. Then, we will copy the reversed elements back to the original array.
main.c
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
int temp[size];
// Store elements in reverse order in temp array
for (int i = 0; i < size; i++) {
temp[i] = arr[size - 1 - i];
}
// Copy back to original array
for (int i = 0; i < size; i++) {
arr[i] = temp[i];
}
// Print the reversed array
printf("Reversed array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Explanation:
- We declare an integer array
arr[]
containing 5 elements. - The variable
size
is used to store the number of elements in the array. - A temporary array
temp[]
of the same size is created to store reversed elements. - In the first loop, we copy elements in reverse order from
arr[]
totemp[]
. - In the second loop, we copy the reversed elements from
temp[]
back toarr[]
. - The final loop prints the reversed array.
Output:
Reversed array: 5 4 3 2 1
2. Reverse an Array by Swapping Elements
In this example, we will use the two-pointer approach to swap elements from the beginning and end until we reach the middle.
main.c
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
int size = sizeof(arr) / sizeof(arr[0]);
int start = 0, end = size - 1, temp;
// Swap elements from both ends
while (start < end) {
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
// Print the reversed array
printf("Reversed array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Explanation:
- We declare an integer array
arr[]
containing 5 elements. - Variables
start
andend
are initialized to the first and last index of the array. - A temporary variable
temp
is used to swap elements. - The
while
loop runs untilstart
is less thanend
, swapping elements atarr[start]
andarr[end]
. - After swapping,
start
is incremented andend
is decremented to move towards the middle. - The final loop prints the reversed array.
Output:
Reversed array: 50 40 30 20 10
3. Reverse an Array Using Recursion
In this example, we will use recursion to swap elements from the beginning and end of the array until the middle is reached.
main.c
#include <stdio.h>
void reverseArray(int arr[], int start, int end) {
if (start >= end)
return;
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
reverseArray(arr, start + 1, end - 1);
}
int main() {
int arr[] = {2, 4, 6, 8, 10};
int size = sizeof(arr) / sizeof(arr[0]);
reverseArray(arr, 0, size - 1);
printf("Reversed array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Explanation:
- The function
reverseArray()
swaps the first and last elements using recursion. - The base condition
start >= end
stops recursion when the middle is reached. - The recursive call moves closer to the middle in each step.
- The
main()
function initializes and prints the reversed array.
Output:
Reversed array: 10 8 6 4 2