Write a program to reverse an array or string C++

The term “reverse of an array” refers to changing the order of the elements in a given array. This approach turns the array’s last element into the first, and the first element into the last. The procedure, however, continues until all of the array’s letters or elements have been entirely reversed.

For example, the array has items such as ‘H’, ‘E’, ‘L’, ‘L’, ‘O’, and when we reverse all of the elements of an array, we get ‘O’, ‘L’, ‘L’, ‘E’, ‘H’. As a result, all of the characters in the array are reversed in this manner.

Various methods for reversing an array

In the C++ programming language, there are several techniques to get the reverse array.

• Using a for loop, reverse an array.
• The reverse() function can be used to reverse an array.
• Using the user-defined function, reverse an array.
• Using the pointers, reverse an array.
• Using the Recursion function, reverse an array.

Program to reverse an array using the for loop

Let’s create a program to reverse the elements of the array using for loop in C++.

Program:

#include
using namespace std;

int main ()
{
int arr[50], num, temp, i, j;
cout << " Please, enter the total no. you want to enter: ";
cin >> num;

// use for loop to enter the numbers
for (i = 0; i < num; i++)
{
cout << " Enter the element " << i+1 << ": ";
cin >> arr[i];
}

for ( i = 0, j = num - 1; i < num/2; i++, j--)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
cout << "\n Reverse all elements of the array: " << endl;
// use for loop to print the reverse array
for ( i = 0; i < num; i++)
{
cout << arr[i] << " ";
}
return 0;
}

Output:

Please, enter the total no. you want to enter: 6
Enter the element 1: 78
Enter the element 2: 12
Enter the element 3: 54
Enter the element 4: 24
Enter the element 5: 7
Enter the element 6: 90

Reverse all elements of the array:
90 7 24 54 12 78

Program to reverse an array using the reverse() function

Let’s consider an example to print the reverse of an array using the reverse () function in C++.

Program:

#include<iostream.h>
#include<conio.h>
using namespace std;

// declare disp() function
void disp(int arr1[], int num)
{
int i;
// use for loop to iterate the characters
for ( i = 0; i < num; i++)
{
cout << arr1[i] << " ";
}
}

// define reverse() function to reverse the array elements
void reverse(int arr1[], int num)
{
reverse(arr1, arr1 + num);
}

int main ()
{
// declare and initialize an array
int arr1[] = {34, 78, 21, 90, 5, 2};
int num = sizeof(arr1)/sizeof(arr1[0]);

// call reverse function and pass parameters
reverse(arr1, num);
disp(arr1, num); /* call disp() function to print the revrse array. */

return 0;
}

Output:

2 5 90 21 78 34

Program to reverse an array using the user-defined function

Let’s consider an example to display the reverse of array elements using the user-defined in C++.

Program:

#include<iostream>
using namespace std;

void ArrRev ( int [], int);
int main ()
{
int arr[50], num, i, j, temp;
cout << " Number of elements to be entered: " << endl;
cin >> num;

cout << " Enter the array elements: " << endl;

// use for loop to enter the elements
for ( i = 0; i < num; i++)
{
cin >> arr[i];
}
cout << " Elements are: \n";
// display entered elements in array
for ( i = 0; i < num; i++)
{
cout << arr[i] << " ";
}
ArrRev (arr, num); // call function

cout << " \n The reverse of the given array is: \n";
// use for loop to print the reverse array elements
for ( i = 0; i < num ; i++)
{
cout << arr[i] << " ";
}
cout << endl;
return 0;
}

void ArrRev ( int ar[], int a2)
{
int i, j, temp;
j = a2 - 1;
for ( i = 0; i < j; i++, j--)
{
temp = ar[i];
ar[i] = ar[j];
ar[j] = temp;
}
}

Output:

Number of elements to be entered:
7
Enter the array elements:
45
32
89
21
78
34
65
Elements are:
45 32 89 21 78 34 65
The reverse of the given array is:
65 34 78 21 89 32 45

Q1. In Java, how do you reverse an array?

In Java, there are three techniques for reversing an array.

• Using a for loop to traverse the array and copy the elements in reverse order to another array.
• Using in-place reversal, which swaps the elements to put them in the opposite order.
• Using the Collections interface’s reverse method, which works with lists.

Q2. In Java, how do you reverse a list?

The reverse function provided by Java’s Collections interface can be used.

Q3. Which method is the most effective for reversing an array?

Normally, the easiest way to reverse an array is to convert it to a list and then use the reverse function. Because it saves memory, in-place reversal is preferable to using another array to reverse the array.