Q. write a program in c++ that display entered string into reverse order.
#include <iostream>
#include <string>
int main()
{
std::cout << "Please input an string: ";
std::string str;
std::getline(std::cin, str);
for(size_t i = 0; i < str.size() / 2; ++i)
{
char tmp = str[i];
str[i] = str[str.size() - 1 - i];
str[str.size() - 1 - i] = tmp;
}
std::cout << "Reversed string is: " << str << "\n";
return 0;
}
Comments
Leave a comment