- #1
FallArk
- 127
- 0
I need to make a piece of code that reverse a string user input.
My solution:
It is not working quite right, where did I do wrong?
My solution:
Code:
#include <iostream>
#include <string>
using namespace std;
string reverseMe(string tmp) {
if (tmp.length() == 1) {
return tmp;
}
else {
reverseMe(tmp.substr(1, tmp.length()));
}
return tmp.substr(1, tmp.length()) + tmp.at(0);
}
int main() {
string tmp;
cout << "Enter a string: ";
getline(cin, tmp);
tmp = reverseMe(tmp);
cout << "REVERSE = [" << tmp << "]" << endl;
}
It is not working quite right, where did I do wrong?