Domanda di colloquio di Microsoft

Code a function to replace, in a given string, a given character with a given substring. Example: Replace("This is it.", 'i', "was"); -> Thwas wass wast." Do it in place.

Risposta di colloquio

Anonimo

28 ott 2016

I was a little bit confused because you cannot really do it in place, since you have to enlarge your string. The interviewer told me to pretend I have a helper function "ResizeString" that puts spaces at the end of the string. The key after that is just to start from the right, copy the characters until you encounter the special char, then replace it with your substring, etc. This is a pretty straight forward question. Computing the size of your new string can be a little bit confusing under the interview stress, but it is simple maths.