Help with this recursion function in C++

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
8 replies · 2K views
FallArk
Messages
127
Reaction score
0
I need to make a piece of code that reverse a string user input.
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?
 
Physics news on Phys.org
You're not doing anything to reverse the string. Are you required to use recursion?
 
greg1313 said:
You're not doing anything to reverse the string. Are you required to use recursion?

So, what should i do?
Yes, recursion is required.
 
Are you allowed to use arrays and pointers?

edit: You can do this without an array and pointers though. My approach would be to pass 3 parameters to the recursively called function, the string, a start position (initially 0) and an end position (initially the length of the string minus 1). Check to see if start >= end, and simply return the string if it is. If not, swap the character at the start position with the character at the end position, then increment the start position, decrement the end position and call the function again in a return of the string.
 
MarkFL said:
Are you allowed to use arrays and pointers?

edit: You can do this without an array and pointers though. My approach would be to pass 3 parameters to the recursively called function, the string, a start position (initially 0) and an end position (initially the length of the string minus 1). Check to see if start >= end, and simply return the string if it is. If not, swap the character at the start position with the character at the end position, then increment the start position, decrement the end position and call the function again in a return of the string.

My instructor didn't say anything about using pointers, so I guess it's okay to use them. The main cannot be changed, so there can only be one parameter
 
FallArk said:
My instructor didn't say anything about using pointers, so I guess it's okay to use them. The main cannot be changed, so there can only be one parameter

MarkFL was suggesting that inside that function call have a call to a second function that has those parameters.
 
I fixed it. Thanks for the help.
My fixed version:
Code:
#include <iostream>
#include <string>

using namespace std;

string reverseMe(string tmp) {
    if (tmp.length() == 1) {
        return tmp;
    }
    else {
        return reverseMe(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;
}
 
Well done! (Yes)
 
Another variant.

Code:
#include <iostream>
#include <string>

using namespace std;

string reverseHelp(string s1, string s2) {
  if (s1.length() <= 1) return s2;
  else return reverseHelp(s1.erase(0, 1), s2.insert(0, 1, s1[0]));
}

string reverseMe(string tmp) {
  return reverseHelp(tmp, "");
}

int main() {
  string tmp;
  cout << "Enter a string: ";
  getline(cin, tmp);
  tmp = reverseMe(tmp);
  cout << "REVERSE = [" << tmp << "]" << endl;
}