Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

C Program to Reverse an Array Elements



Reversing an array means changing the order of its elements so that the first element becomes the last, the second becomes the second last, and so on. This operation is commonly used in applications such as data manipulation or algorithms that require elements to be in reverse order.

For example, consider an array of integers:

arr[] = {1, 2, 3, 4, 5}

When the array is reversed, the first element (1) moves to the last position, the second element (2) becomes the second last, and so on. The reversed array will be:

arr[] = {5, 4, 3, 2, 1}

Our task is to find a method to reverse the elements of an array in C, so that the first element becomes the last. In this article, we'll discuss different ways to do this in C.

How to Reverse an Array in C

In this article, we will discuss three approaches to reverse an array in C ?

Using a Temporary Array

In this approach, we use a temporary array to store the reversed version of the original array. We then copy the elements back to the original array from the temporary array.

Example

We first create a new temporary array. Then, starting from the last element of the original array, we copy the elements into the temporary array in reverse order. Finally, we copy the reversed elements back into the original array.

#include <stdio.h>

void reverseArray(int arr[], int size) {
    int temp[size]; // Temporary array
    int j = 0;

    // Copy elements in reverse order
    for (int i = size - 1; i >= 0; i--) {
        temp[j++] = arr[i];
    }

    // Copy reversed elements back to the original array
    for (int i = 0; i < size; i++) {
        arr[i] = temp[i];
    }
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int size = sizeof(arr) / sizeof(arr[0]);

    // Display the original array
    printf("Original array: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("
"); // Reverse the array reverseArray(arr, size); // Display the reversed array printf("Reversed array: "); for (int i = 0; i < size; i++) { printf("%d ", arr[i]); } printf("
"); return 0; }

The output displays the original array after it has been reversed.

Original array: 1 2 3 4 5 
Reversed array: 5 4 3 2 1 

Time Complexity: O(n), where n is the size of the array (due to the two loops).

Space Complexity: O(n), as we use an extra temporary array of the same size.

Using Two Pointers

In this approach, we use two pointers, one starting from the beginning of the array and the other from the end. We swap the elements at the two pointers and move them towards each other until they meet in the middle.

Example

We initialize two pointers, one at the start (left) and the other at the end (right) of the array. Then, we swap the elements at these positions and move the pointers towards each other until they meet in the center.

#include <stdio.h>

void reverseArray(int arr[], int size) {
    int left = 0;
    int right = size - 1;

    // Swap elements using two pointers
    while (left < right) {
        int temp = arr[left];
        arr[left] = arr[right];
        arr[right] = temp;

        left++;
        right--;
    }
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int size = sizeof(arr) / sizeof(arr[0]);

    // Display the original array
    printf("Original array: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("
"); // Reverse the array reverseArray(arr, size); // Display the reversed array printf("Reversed array: "); for (int i = 0; i < size; i++) { printf("%d ", arr[i]); } return 0; }

The output displays the reversed array after performing the swap.

Original array: 1 2 3 4 5 
Reversed array: 5 4 3 2 1

Time Complexity:O(n), as we swap each element once.

Space Complexity:O(1), as no extra space is used except for the swap operation.

Using Recursion

In this approach, we use recursion to reverse the array. The recursive function swaps the elements at the beginning and end of the array and then calls itself with a smaller subarray.

Example

We define a recursive function that swaps the first and last elements, then calls itself with the next set of elements, moving toward the middle.

#include <stdio.h>

void reverseArray(int arr[], int left, int right) {
    if (left >= right) return;  // Base case: If pointers cross, stop recursion

    // Swap elements
    int temp = arr[left];
    arr[left] = arr[right];
    arr[right] = temp;

    // Recursive call for the next elements
    reverseArray(arr, left + 1, right - 1);
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int size = sizeof(arr) / sizeof(arr[0]);

    // Display the original array
    printf("Original array: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("
"); // Reverse the array reverseArray(arr, 0, size - 1); // Display the reversed array printf("Reversed array: "); for (int i = 0; i < size; i++) { printf("%d ", arr[i]); } return 0; }

The output displays the reversed array after recursion has swapped the elements.

Original array: 1 2 3 4 5 
Reversed array: 5 4 3 2 1 

Time Complexity: O(n), as we make a recursive call for each element.

Space Complexity: O(n), due to the function call stack in recursion.

Conclusion

In this article, we looked at three ways to reverse an array in C: using a temporary array (easy but needs extra space), using two pointers (fast and doesn't need extra space), and using recursion (clean but less efficient for large arrays). Each method has its pros, and the choice depends on memory and ease of use.

Updated on: 2025-02-14T18:05:30+05:30

92K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements