Hey everybody,
So I'm working on a homework problem.
We are implementing a bunch of different functions to manipulate and test c-strings. The catch is we are doing every function two ways: iteratively and recursively. I have done all of the other functions both ways but I'm stuck on recursive reverse function.
Okay so I've been trying to work this out for several hours now and I seem to be going in circles.
The problems I'm dealing with are:
1.) it has to be a void function
2.) the only arguments it can take are the source c-string(forward) and destination c-string(to be in reverse).
It really would be a very simple if I didn't have to worry about those things.
So this is what I kind of left off on. It doesn't reverse the string but copies it over which was something.
1 2 3 4 5 6 7 8 9 10 11
|
void str_reverse_r ( const char str1[], char str2[] )
{
/*
* this only works when we assume that str2 is empty at start
*/
if( (*str1)!='\0' ){
str_reverse_r(str1+1, str2+1);
}
(*str2)=(*str1);
return;
}
| |
If someone could give me a few pointers or talk me through some pseudo code I would really appreciate it.
And here is a program to run the function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
#include <iostream>
using namespace std;
void str_reverse_r ( const char str1[], char str2[] )
{
/*
* this only works when we assume that str2 is empty at start
*/
if( (*str1)!='\0' ){
str_reverse_r(str1+1, str2+1);
}
(*str2)=(*str1);
return;
}
int main(){
char test[100]="abcdef";
char fill[100]="";
cout<< "String ::"<< test<< "::\n\n";
fill[0] = '\0';
str_reverse_r(test, fill);
cout << "recursive reverse::"<< fill << "::\n";
char p;
cin>> p;
return 0;
}
| |
Thank you in advance.