Glad you found that helpful. I can't help much with your mix function. It's supposed to return a string, but only does so on line 12. When it returns following line 18 what value is returned? If mix returns a value it's not being utilized on line 16.
What kind of strings are you using? I had assumed the c++ string class, but clearly not. I see no #include for anything.
The terminating condition based on finding the '\0' should work fine in the function I posted:
1 2 3 4 5 6 7 8
|
void show_ofer( const string& s, size_t i=0 )
{
if( s[i] == '\0' ) return;
if( i%2 == 1 ) std::cout << s[i];// odd i on the way in
show_ofer( s, i+1 );
if( i%2 == 0 ) std::cout << s[i];// even i on the way out
}
|
That mix function is gonna be your baby. Why does it need to stream:
cout << mix(s,i,l) << endl;
. There are cout statements in the function. You could just call it:
mix(s);
then it doesn't have to return a string.
edit: Do you want the function to produce a string with elements in the given order, then cout the string?
This version will produce a string Copy:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
void getStr_ofer( string& Copy, const string& sce, size_t i=0 )
{
if( sce[i] == '\0' ) return;
if( i%2 == 1 ) Copy += sce[i];
getStr_ofer( Copy, sce, i+1 );
if( i%2 == 0 ) Copy += sce[i];
}
int main()
{
string copyStr;
getStr_ofer( copyStr, "abcdef" );
cout << '\n' << copyStr;
return 0;
}
|
Operator += maybe not supported by your string class? push_back(string)? append(string)? Any of those would do.