Javatpoint Logo
Javatpoint Logo

Find Reverse of an Array in C Using Functions

This article will demonstrate how to create a C program that uses a function to reverse an array. For instance, if "arr" is an array of three numbers like in:

Then, by reversing the array we will have:

In C, you may reverse an array in four different ways: with a for loop, pointers, recursion, or by writing a function.

Example:

Output:

?nter size of array: 3  
?nter the elements of the array: 1 2 3
After reversing the array: 3 2 1
....................................
Process executed in 1.22 seconds
Press any key to continue.

Explanation

In the above example of a program in C, we have demonstrated how we can use function to reverse an array. We have created a user defined function reverse which takes the array and the size of the array as parameter and creates a tmp variable to swap the numbers.

In-Place Implementation

Output:

5 4 3 2 1
.............
Process executed in 2.12 seconds
Press any key to continue. 

Explanation

In the above example of a program in C, reading the items from both ends of the array and swapping them can be used to build a linear in-place algorithm, as shown in the example.

Another Example:

Output:

5 4 3 2 1
....................
Process executed in 1.22 seconds
Press any key to continue.

Explanation

Another is a recurring system that puts the elements in a call stack before returning them to the same members in the correct order at the end of the repetition.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA