![]() |
VOOZH | about |
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.
5 4 3 2 1
Apart from the above method, an array can also be reversed using the methods shown below:
Table of Content
This method is the recursive implementation of two pointer approach but uses recursive call to swap the elements and move the two pointers.
5 4 3 2 1
Another 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.
5 4 3 2 1