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

</>
Copy
#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:

  1. We declare an integer array arr[] containing 5 elements.
  2. The variable size is used to store the number of elements in the array.
  3. A temporary array temp[] of the same size is created to store reversed elements.
  4. In the first loop, we copy elements in reverse order from arr[] to temp[].
  5. In the second loop, we copy the reversed elements from temp[] back to arr[].
  6. 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

</>
Copy
#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:

  1. We declare an integer array arr[] containing 5 elements.
  2. Variables start and end are initialized to the first and last index of the array.
  3. A temporary variable temp is used to swap elements.
  4. The while loop runs until start is less than end, swapping elements at arr[start] and arr[end].
  5. After swapping, start is incremented and end is decremented to move towards the middle.
  6. 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

</>
Copy
#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:

  1. The function reverseArray() swaps the first and last elements using recursion.
  2. The base condition start >= end stops recursion when the middle is reached.
  3. The recursive call moves closer to the middle in each step.
  4. The main() function initializes and prints the reversed array.

Output:

Reversed array: 10 8 6 4 2