Subj : Re: Reverse words in a string (Another Interview question) To : comp.programming From : Sc0rpi0 Date : Thu Sep 29 2005 09:40 pm Willem: > Anyway, the simplest way, and probably the most efficient, would be to > allocate room for a new string, and then just copy the string word by > word, in reverse order. Simplest I think is not to allocate room for a new string. Just reverse, first in words wchich we just simply find (start-end), each one reverse using half of word length number of steps: like blahblah-> hlahblab->...->hlabhlab ^ ^ ^ ^ ^^ then reverse entire string same way. Not too slow, and no extra memory is used - i think it's OK. Of course it is slower than copying right away in reverse order, and cannot be done with consts (i.e. "string example" in C), but is simple and nice and uses no memory extra. Anoter one way is to search words from two ends, and replace at once. But have no time to think about it right now ;P. (and yes, I know words don't have to be equal length). -- Sc0rpi0 I hated going to weddings. All the grandmas would poke me saying "You're next". They stopped that when I started doing it to them at funerals. .