Write a program to take an input string, store this string into another string in reverse order. Print the original and reversed string both.
#include <iostream>
#include <string>
int main()
{
std::cout << "Please enter an string: ";
std::string source;
std::getline(std::cin, source);
std::string result;
for(std::string::const_reverse_iterator iter = source.rbegin(); iter != source.rend(); ++iter)
{
result.push_back(*iter);
}
std::cout << "Original string: " << source << "\n";
std::cout << "Reversed string: " << result << "\n";
return 0;
}
Comments
Leave a comment