Reverse an array in C
×


Reverse an array in C

256

 Reversing an array involves rearranging its elements in such a way that the final element becomes the initial one, the second-to-last element becomes the second one, and so on.

 This process essentially flips the order of the array's elements.

 This task is commonly encountered in programming and has multiple ways to accomplish it.

 To reverse an array in C, there are various approaches available, each with its own method and implementation.

Approaches to Reverse an Array  

1Using an Additional Array:

In this approach, we create a new array and copy elements from the original array into it in reverse order.

Example:

// Program for Additional of Array
#include<stdio.h> 

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

    // Reversing the array
    for (int i = size - 1, j = 0; i >= 0; i--, j++) {
        reversed[j] = arr[i];
    }

    // Printing the reversed array
    printf("Reversed Array: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", reversed[i]);
    }
    return 0;
}

Output:

Reversed Array: 5 4 3 2 1 

2Iteration and Swapping:

Here, we iterate through the array and swap elements from the beginning with those from the end until we reach the middle.

Example:

// Program for Iteration and Swapping
#include<stdio.h>

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

    // Reversing the array
    for (int i = 0, j = size - 1; i < j; i++, j--) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

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

Output:

Reversed Array: 5 4 3 2 1 

3Iterating from Two Ends:

This method involves traversing the array from both ends towards the center, swapping elements along the way.

Example:

// Program for Iterating from two ends
#include<stdio.h>

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

    // Reversing the array
    while (leftside < rightside) {
        int temp = arr[leftside];
        arr[leftside] = arr[rightside];
        arr[rightside] = temp;
        leftside++;
        rightside--;
    }

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

Output:

Reversed Array: 5 4 3 2 1 

4Using Pointers:

Here, we utilize pointers to swap array elements without needing additional indices.

Example:

// Program for using pointer
#include<stdio.h>
int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int size = sizeof(arr) / sizeof(arr[0]);
    int *left = arr, *right = arr + size - 1;

    while (left < right) {
        int temp = *left;
        *left = *right;
        *right = temp;
        left++;
        right--;
    }

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

Output:

Reversed Array: 5 4 3 2 1 

5Recursive Approach:

In this approach, we recursively reverse the array by swapping elements from the outer ends towards the center.

Example:

// Program for Recursive Approach 
#include<stdio.h>

void reverseArray(int arr[], int left, int right) {
    if (left >= right) {
        return;
    }
    int temp = arr[left];
    arr[left] = arr[right];
    arr[right] = temp;
    reverseArray(arr, left + 1, right - 1);
}

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

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

Output:

Reversed Array: 5 4 3 2 1 


Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments