Reverse Array in C Last Updated : 20 Nov, 2024 Suggest changes Share Like Article Like Report Reversing an array means the swapping last element and first element, second last element and second element, and so on. In this article, we will learn how to reverse an array in C.The simplest method to reverse an array in C program is by using two pointers: one starting at the beginning (left) and one at the end (right) of the string. Swap the elements at these pointers while moving them towards centre of the array until the pointers meet. C #include <stdio.h> void rev(int arr[], int n) { // Two pointers int l = 0, r = n - 1; while (l < r) { // Swap the elements int temp = arr[l]; arr[l] = arr[r]; arr[r] = temp; // Move pointers towards middle l++; r--; } } int main() { int arr[] = {1, 2, 3, 4, 5}; int n = sizeof(arr) / sizeof(arr[0]); // Reverse array arr rev(arr, n); for (int i = 0; i < n; i++) printf("%d ", arr[i]); return 0; } Output5 4 3 2 1 Apart from the above method, an array can also be reversed using the methods shown below:Table of ContentUsing RecursionUsing a Temporary ArrayUsing RecursionThis method is the recursive implementation of two pointer approach but uses recursive call to swap the elements and move the two pointers. C #include <stdio.h> void rev(int arr[], int l, int r) { if (l >= r) { return; } // Swap the elements int temp = arr[l]; arr[l] = arr[r]; arr[r] = temp; // Recursively call function to reverse the // remaining part rev(arr, l + 1, r - 1); } int main() { int arr[] = {1, 2, 3, 4, 5}; int n = sizeof(arr) / sizeof(arr[0]); // Reverse the array arr rev(arr, 0, n - 1); for (int i = 0; i < n; i++) printf("%d ", arr[i]); return 0; } Output5 4 3 2 1 Using Temporary ArrayAnother method is to create a new array to store the reversed elements by copying the array from last to first, then copy the contents back into the original array. C #include <stdio.h> void rev(int arr[], int n) { int temp[n]; // Store elements into temp in reverse order for (int i = 0; i < n; i++) { temp[i] = arr[n - 1 - i]; } // Copy reversed array back to original array 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]); // Reverse the array arr rev(arr, n); for (int i = 0; i < n; i++) printf("%d ", arr[i]); return 0; } Output5 4 3 2 1 Advertise with us Next Article Reverse Array in C K kartik Follow Similar Reads Reverse String in C In C, reversing a string means rearranging the characters such that the last character becomes the first, the second-to-last character becomes the second, and so on. In this article, we will learn how to reverse string in C.The most straightforward method to reverse string is by using two pointers t 3 min read How to Reverse a String in C? In C, a string is a sequence of characters terminated by a null character (\0). Reversing a string means changing the order of the characters such that the characters at the end of the string come at the start and vice versa. In this article, we will learn how to reverse a string in C. Example: Inpu 2 min read C Program to Traverse an Array in Reverse Write a C program to traverse a given array in reverse order that contains N elements.ExamplesInput: arr[] = {2, -1, 5, 6, 0, -3}Output: -3 0 6 5 -1 2Input: arr[] = {4, 0, -2, -9, -7, 1}Output: 1 -7 -9 -2 0 4Different Ways to Traverse an Array in Reverse Order in CWe can traverse/print the array in 2 min read Reverse Number Program in C The reverse of a number of means reversing the order of digits of a number. In this article, we will learn how to reverse the digits of a number in C language. Example:Input: 12354Output: 45321Explanation: The number 12354 when reversed gives 45321Input: 623Output: 326Explanation: The number 623 whe 2 min read C Program to Reverse a Stack using Recursion Write a program to reverse a stack using recursion, without using any loop. Example:Â Input: elements present in stack from top to bottom 1 2 3 4Â Output: 4 3 2 1Â Input: elements present in stack from top to bottom 1 2 3Output: 3 2 1 Recommended PracticeReverse a StackTry It!Reverse a stack using Re 5 min read Article Tags : C Programs C Language c-array C Strings Programs C Array Programs C Examples +2 More Like