Help with this recursion function in C++

Click For Summary

Discussion Overview

The discussion revolves around creating a recursive function in C++ to reverse a user-input string. Participants explore different approaches to implementing recursion, the use of parameters, and the constraints of the assignment.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant presents an initial implementation of a recursive string reversal function but notes that it is not working correctly.
  • Some participants point out that the initial code does not effectively reverse the string and question whether recursion is a requirement.
  • Another participant suggests an alternative approach using three parameters in the recursive function, including start and end positions, while also considering the possibility of using arrays and pointers.
  • One participant mentions that their instructor did not specify restrictions on using pointers, implying they may be acceptable.
  • A later reply indicates a successful revision of the initial code, which correctly implements the recursive string reversal.
  • Another participant shares a different variant of the recursive function that utilizes a helper function to achieve the same goal.

Areas of Agreement / Disagreement

Participants generally agree on the need for recursion in the solution, but there are multiple competing views on the implementation details and whether to use additional parameters or helper functions. The discussion remains unresolved regarding the optimal approach.

Contextual Notes

Some participants express uncertainty about the use of arrays and pointers, and there are unresolved questions about the constraints of the assignment, particularly regarding the number of parameters allowed in the main function.

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

Replies
12
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 118 ·
4
Replies
118
Views
10K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K