Answer:
C++.
#include <iostream>
#include <string>
using namespace std;
///////////////////////////////////////////////////////////////////
string add_spaces(string input) {
if (input.length() < 2)
return input;
else {
string minus_input = input.substr(0, input.length()-1);
return add_spaces(minus_input) + " " + input[input.length()-1];
}
}
///////////////////////////////////////////////////////////////////
int main() {
cout<<add_spaces("hangman");
return 0;
}
///////////////////////////////////////////////////////////////////
Explanation:
A Recursive function calls itself inside its body. To prevent it from running forever, there's a break condition inside recursive functions.
In our example it was a single character string.
We use recursion to solve problems by breaking them into smaller pieces.
In our example, we broke down the original string from the last character, till it only has its starting character. When the last character was encountered, we returned as it is in the break condition.
From there onward, all characters were getting separated by a space, till we reach the first add_spaces function call, where we return the full string with space between them.