Reverse Array in C Last Updated : 20 Nov, 2024 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice 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 Comment More infoAdvertise with us Next Article Reverse Array in C kartik Follow Improve Article Tags : C Programs C Language c-array C Strings Programs C Array Programs C Examples +2 More 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 C Program to Reverse a String Using Recursion Reversing a string means changing the order of characters in the string so that the last character becomes the first character of the string. In this article, we will learn how to reverse a string using recursion in a C program.The string can be reversed by using two pointers: one at the start and o 1 min read C Program To Reverse Words In A Given String Example: Let the input string be "i like this program very much". The function should change the string to "much very program this like i" Examples:Â Input: s = "geeks quiz practice code"Â Output: s = "code practice quiz geeks" Input: s = "getting good at coding needs a lot of practice"Â Output: s = " 3 min read C Program for Reversal algorithm for array rotation Write a function rotate(arr[], d, n) that rotates arr[] of size n by d elements. Example: Input: arr[] = [1, 2, 3, 4, 5, 6, 7] d = 2 Output: arr[] = [3, 4, 5, 6, 7, 1, 2] Rotation of the above array by 2 will make array Algorithm : rotate(arr[], d, n) reverse(arr[], 1, d) ; reverse(arr[], d + 1, n); 3 min read C Program To Print Reverse Floyd's Pattern The Reverse Floyd's Triangle Pattern prints numbers or characters in reverse order, starting with the largest at the top and decreasing as the rows progress. Basically, it is Floyd's triangle that starts with the largest number and ends with 1. In this article, we will learn how to print the Reverse 2 min read C Program to reverse the digits of a number using recursion Given an integer N, the task is to reverse the digits of given integer using recursion. Examples: Input: N = 123Output: 321Explanation:The reverse of the given number is 321. Input: N = 12532Output: 23521Explanation:The reverse of the given number is 23521. Approach: Follow the steps below to solve 2 min read Like