The point of the exercise is to think about what actions you need to perform to get the desired results, and what data structures there are to support those actions.
This is one of those cases where it's all about the journey, not the destination
For example, you've got the problem that you're adding a space after each word, but there should only be spaces between them. What's the difference between those two? The difference is that the last word should not get a space after it, so you'd need to test for that, for example by counting how many words you've processed and comparing to how many there are.
Computers operate in the same universe as us, the same laws of physics/logic apply, operations that you can think up can also be implemented by the computer as long as you describe them sufficiently