Help with this recursion function in C++

Click For Summary
SUMMARY

This discussion focuses on implementing a recursive function in C++ to reverse a user-input string. The original code provided by the user was incorrect due to improper handling of string manipulation within the recursion. The corrected approach involves using a helper function that takes two parameters: the string to be reversed and an accumulator for the reversed string. The final solution utilizes the erase and insert methods to achieve the desired result efficiently.

PREREQUISITES
  • Understanding of C++ string manipulation functions such as substr, erase, and insert.
  • Familiarity with recursion and its implementation in C++.
  • Basic knowledge of C++ syntax and structure, including function definitions and main function execution.
  • Experience with handling user input in C++ using getline.
NEXT STEPS
  • Study C++ recursion techniques and best practices for string manipulation.
  • Learn about the C++ Standard Library functions for string handling, focusing on std::string.
  • Explore alternative methods for reversing strings in C++, such as using iterators or the reverse algorithm from the algorithm header.
  • Investigate performance implications of different string manipulation techniques in C++.
USEFUL FOR

C++ developers, computer science students, and anyone interested in mastering recursive algorithms and string manipulation 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

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