C++ Program to Reverse a String:Reverse of String means to reverse the position of all character of any String. For example reverse of porter is retrop.To reverse a string in C++ programming, then ask to the user to enter a string, now make a variable say temp of char type.
C++ Program to Reverse a String
#include <iostream>
using namespace std;
void reverse(const string& a);
int main()
{
string str;
cout << " Please Enter a String " << endl;
getline(cin, str);
reverse(str);
return 0;
}
void reverse(const string& str)
{
size_t numOfChars = str.size();
if(numOfChars == 1)
cout << str << endl;
else
{
cout << str[numOfChars - 1];
reverse(str.substr(0, numOfChars - 1));
}
}
Output of Program
Please Enter a String :
Code Blah To Learn Programming
gnimmargorP nraeL oT halB edoC