Un-mash a string in C++ using recursion

Part A: Have a user input a string. Then display this string smashed up as follows: display the first character in the string, then the last, then the second, then the second to last, then the third... So if the string is “abcdef”, it will display:
afbecd (input “abcdef”)
12345 --> 15243
123456 --> 162534

Part B: Now, unmash the above strings.
i.e 162534 -->123456
I got part A to work.

#include <iostream>
using namespace std;
void mash(string s);

int main()
{
string sequence;
cout << "Enter a sequence: ";
getline(cin, sequence);
mash(sequence);
}

void mash(string s)
{
int a = s.length();

if (a == 0)
{
return;
}
if (a == 1)
{
cout << s;
return;
}

cout << s[0];

if(a>1)
{
cout << s[a - 1];
s = s.substr(1,a-2);
mash(s);

}
}
but I have no clue how to approach part B. I guess I can try to print out the characters in the even position, say in the string 162534, thus I will get 123. Then I guess I can try to print out the odd position characters from the last one up to the first one, i.e, 456. Combining these two will get the original strings but I have no clue how to use recursion to solve part B.
Mash and un-mash are very similar.

mash: first character + last character + mash of the substring in between

unmash: first character + unmash of the tail + second character

Spoiler: http://coliru.stacked-crooked.com/a/f7db3eada7526a91
Topic archived. No new replies allowed.