C/C++ Help with this recursion function in C++

AI Thread Summary
The discussion centers around creating a recursive function in C++ to reverse a user-input string. The original code provided fails to reverse the string correctly due to improper handling of recursion and string manipulation. Participants clarify that recursion is required, and suggest an improved approach that involves passing the start and end positions of the string to facilitate character swapping. A revised version of the code is shared, which successfully implements the recursive logic to reverse the string. Another variant is also presented, utilizing a helper function to achieve the same result. Overall, the conversation emphasizes the importance of correctly structuring recursive calls and string operations in C++.
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?
 
Technology 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;
}
 

Similar threads

Back
Top