There are very few standard C functions
(memmove being a notable exception) which work reliably when the input and output buffers overlap. In essence, if you change the thing you're reading from, then expect surprises (sometimes the surprise comes years later).
This for example is likely to just keep going until it runs off the end of memory (or trashes something) and then it crashes.
Since you used cout, why not use std::string for all your strings. It's much safer.