Reversing Arrays in C
A Step-by-Step Guide

Reversing an array is a common operation in programming, and C is no exception. In this article, we will explore different methods to reverse an array in C, along with examples and explanations.

Method 1: Using a Temporary Array

One way to reverse an array is to create a temporary array and copy the elements of the original array to the temporary array in reverse order.

#include <stdio.h>

void reverseArray(int arr[], int n) {
    int temp[n];
    for (int i = 0; i < n; i++) {
        temp[i] = arr[n - i - 1];
    }
    for (int i = 0; i < n; i++) {
        arr[i] = temp[i];
    }
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);
    printf("Original array: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
    reverseArray(arr, n);
    printf("Reversed array: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
    return 0;
}

In this example, we define a function reverseArray that takes an array arr and its size n as arguments. We create a temporary array temp of the same size as the original array. We then iterate through the original array and copy each element to the corresponding position in the temporary array, but in reverse order. Finally, we copy the elements from the temporary array back to the original array.

Method 2: Swapping Elements

Another way to reverse an array is to swap the elements of the array in place, without using a temporary array.

#include <stdio.h>

void reverseArray(int arr[], int n) {
    for (int i = 0; i < n / 2; i++) {
        int temp = arr[i];
        arr[i] = arr[n - i - 1];
        arr[n - i - 1] = temp;
    }
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);
    printf("Original array: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
    reverseArray(arr, n);
    printf("Reversed array: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
    return 0;
}

In this example, we define a function reverseArray that takes an array arr and its size n as arguments. We iterate through the first half of the array and swap each element with the corresponding element from the end of the array.

Method 3: Using Recursion

We can also reverse an array using recursion.

#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[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);
    printf("Original array: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
    reverseArray(arr, 0, n - 1);
    printf("Reversed array: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
    return 0;
}

In this example, we define a function reverseArray that takes an array arr, a start index start, and an end index end as arguments. We swap the elements at the start and end indices, and then recursively call the function with the start index incremented and the end index decremented.

Conclusion

Reversing an array is a common operation in programming, and C provides several ways to do it. In this article, we explored three methods to reverse an array in C: using a temporary array, swapping elements, and using recursion. Each method has its own advantages and disadvantages, and the choice of method depends on the specific use case.

ShareTwitterShareFacebookShareLinkedin

🌻 Latest Blog Posts: Stay Informed and Inspired

Explore the latest and greatest from our blog! Dive into a diverse range of topics.

Mastering PHP: Sending GET Requests with Ease

explore the power of PHP in making HTTP GET requests, using the popular JSONPlaceholder API as an example. You'll learn how to send GET requests, handle the response, and extract valuable data.

Understanding Pointers in C Programming with Examples

Explore the fundamentals of pointers in C programming with detailed examples and explanations. Learn how to use pointers effectively in your C code.

Reversing Arrays in C: A Step-by-Step Guide

Learn how to reverse arrays in C programming with examples and explanations. Understand the different methods to reverse an array, including using a temporary array, swapping elements, and using recursion.

Reversing Strings in C Programming: A Step-by-Step Guide

Learn how to reverse strings in C programming with examples and code snippets. Discover three methods: temporary array, two pointers, and recursion.