Reversed Strings

So I'm supposed to write a program that asks the user to enter a string and then passes it to a void recursive function which displays the string in reverse. The prototype of the function would be:void reverse(string s, int i);
The call would be:reverse(strln, strln.length());

-This is what have so far
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  #include <iostream>
#include <string>

using namespace std;

void reverse(string s, int i); // Function prototype

int main()
{
	string choice;
	int i;
	cout << "Enter a string: " << endl;
	getline(cin, choice);
	
	cout << "The reversed string is "; reverse(choice, i);
	
	return 0;
}

void reverse(string s, int i)
{
	reverse(strln, strln.length());
}
Hello ohsimplyme,

Line 15 has one if not two problems. First you might have an extra semi-colon before reverse. Second the reverse function returns nothing, so if you expected to print something it will not work unless you are going to print from the function, then line 15 is fine.

In the reverse function line 22 will call the reverse function in an endless loop. But since "strln" is never defined, so it will not even compile. As a recursive function there is no way out and you do nothing to reverse the string passed to the function.

Hope that helps,

Andy
Hello @ohsimplyme, in addition of what @Handy said,
i hope this example helps you to understand how a
recursive function works;


Calling numbers(9)
Calling numbers(8)
Calling numbers(7)
Calling numbers(6)
Calling numbers(5)
Calling numbers(4)
Calling numbers(3)
Calling numbers(2)
Calling numbers(1)
Printing from numbers(0) ->1 
Printing from numbers(1) ->2 
Printing from numbers(2) ->3 
Printing from numbers(3) ->4 
Printing from numbers(4) ->5 
Printing from numbers(5) ->6 
Printing from numbers(6) ->7 
Printing from numbers(7) ->8 
Printing from numbers(8) ->9 
Printing from numbers(9) ->10


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

void numbers(int n);

int main(int argc, const char * argv[]) {
    
   
    numbers(10);
    
    std::cout<<"\n";
    
    return 0;
}

void numbers(int n)
{
    if(n > 1)
    {
        std::cout<<"\nCalling numbers(" <<n-1 <<")";
        numbers(n-1);
    }
    
    std::cout<<"\nPrinting from numbers(" <<n-1 <<") ->"<<n <<' ';
}
Hello Andy, Thank you so much for your response. I'm still a bit confused on how to fix the problem in line 22, our prof actually gave us that in order to use but I'm not sure how to proceed.
@ohsimplyme,

Try a specific example: reverse the string "Hello".

This would give: "o" + reverse of "Hell" [sic] - 4 remaining letters
which is:        "o" + "l" + reverse of "Hel" - 3 remaining letters
which is:        "o" + "l" + "l" + reverse of "He" - 2 remaining letters
which is:        "o" + "l" + "l" + "e" + reverse of "H" - 1 remaining letter

When it gets down to 1 remaining letter it doesn't need to reverse anything, just return that letter.


So, for a recursive function to reverse a string of N letters:
- if N is 1, just return the one-character string;
- otherwise, return a string formed from 'last letter of string' + reverse of substring with N-1 letters.

Since the recursion reduces N by 1 each time, it will work its way down to 1, then return back in the opposite order (as @eyenriques' example shows).

Check out the string methods (particularly substr() ) on this site.

Incidentally, if you have to use a void function, then the argument will have to be a reference (use an &s, not s in the argument list). Alternatively, use a string return type, rather than void.


A couple of other comments:
- I guess that your teacher has specified the form of the function so you don't have much option, but it wouldn't actually be vital to pass integer N: this could be deduced automatically from the length of the string;
- it seems a rather inefficient way of reversing a string!
Last edited on
Hello ohsimplyme,

After reading your response and the other messaged I realized that the "reverse" function is what you have to use. That bit of code is a start, but you will need to add to it to work correctly. eyenrique had shown you an example the will work, but it will need to be changed to use a string with a subscript, so the "int i" as a parameter becomes useful. It will need to be set to the last element of the string before the function is called and eyenrique's if statement will have to change to to reach element 0 of the string.

All I added was three lines of code to what you started with to make the program work.

Five it a shot and post your changes if you have a problem. You may want to do a search here or on Google for recursion to see what others have done. I know there is a lot of information here on the subject.

Hope that helps,

Andy

P.S. It would be a good idea to change the name of the function because there are one or more uses of "reverse" in the standard libraries.
Last edited on
Topic archived. No new replies allowed.