I'm learning how to write recursions, and I am confused as to how to simplify the body of a function into a recursion.
For my current assignment, I have "to Mesh two strings by alternating characters from them. If one string runs out before the other, just pick from the longer one. For example, mesh("Fred", "Wilma") is "FWrieldma". Use recursion. Do not use loops."
Well... I created the loop....
string result;
for (int i = 0; i < a.size(); ++i)
{
result += a.at(i) + b.at(i)
}
But making that into a recursion is stumping me.
This is what I have so far (We are not allowed to change anything above or below where it is marked):
#include <string>
using namespace std;
/**
Combines alternate characters from each string.
@param a the first string.
@param b the second string
*/
string mesh(string a, string b)
{
// INSERT CODE BENEATH HERE
string result;
if (a.size() < 1) result = b;
if (b.size() < 1) result = a;
else
{
result = a.at(0) + b.at(1);
}
return result;
// ENTER CODE ABOVE HERE
}
But i know its not right because there is no recursion and it flat out doesn't work