Help with this recursion function in C++

In summary, the instructor recommends using a function that takes 3 parameters. One of the parameters is the string to be reversed, a start position, and an end position. The second function is used to erase the first character of the string and insert the second character at the position specified.
  • #1
FallArk
127
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
  • #2
You're not doing anything to reverse the string. Are you required to use recursion?
 
  • #3
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.
 
  • #4
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.
 
  • #5
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
 
  • #6
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.
 
  • #7
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;
}
 
  • #8
Well done! (Yes)
 
  • #9
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;
}
 

FAQ: Help with this recursion function in C++

What is a recursion function in C++?

A recursion function in C++ is a function that calls itself repeatedly until a certain condition is met. It is a programming technique where a function solves a problem by breaking it down into smaller subproblems of the same type.

How does a recursion function work in C++?

A recursion function works by calling itself with a smaller input until it reaches the base case, which is a condition that stops the function from calling itself again. Once the base case is reached, the function starts returning values and the previous function calls are resolved one by one.

What are the advantages of using recursion functions in C++?

Recursion functions can simplify complex problems by breaking them down into smaller, more manageable subproblems. They also eliminate the need for nested loops, making the code easier to read and understand. Additionally, some problems are inherently recursive, making it the most efficient solution.

What are the common mistakes to avoid when using recursion functions in C++?

Some common mistakes to avoid when using recursion in C++ include forgetting to include a base case, causing an infinite loop, or not breaking the problem down into smaller subproblems. It is also important to manage memory properly to avoid stack overflow errors.

Can recursion functions be used in all programming languages?

Yes, recursion functions can be used in most programming languages, including C++. However, some languages may not support recursion due to memory limitations or restrictions on function calls. It is important to check the language specifications before using recursion in a program.

Similar threads

Replies
118
Views
8K
Replies
22
Views
3K
Replies
8
Views
2K
Replies
8
Views
2K
Replies
5
Views
2K
Replies
40
Views
3K
Replies
10
Views
2K
Back
Top