Answer:
Following are the program to this question:
#include <iostream> //defining header file
using namespace std;
void reverse (string a) //defining method reverse
{
for (int i=a.length()-1; i>=0; i--) //defining loop that reverse string value
{
cout << a[i]; //print reverse value
}
}
int main() //defining main method
{
string name; // defining string variable
cout << "Enter any value: "; //print message
getline(cin, name); //input value by using getline method
reverse(name); //calling reverse method
return 0;
}
Output:
Enter any value: ABCD
DCBA
Explanation:
In the above program code, a reverse method is declared, that accepts a string variable "a" in its arguments, inside the method a for loop is declared, that uses an integer variable "i", in which it counts string value length by using length method and reverse string value, and prints its value.
- In the main method, a string variable "name" is declared, which uses the getline method.
- This method is the inbuilt method, which is used to input value from the user, and in this, we pass the input value in the reverse method and call it.