Re: Reverse words in a string (Another Interview question)
- From: Sc0rpi0 <sc0rpi0@xxxxxxxxxxxxxx>
- Date: Thu, 29 Sep 2005 20:40:28 +0200
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.
.
- Follow-Ups:
- References:
- Reverse words in a string (Another Interview question)
- From: Jaspreet
- Re: Reverse words in a string (Another Interview question)
- From: Willem
- Reverse words in a string (Another Interview question)
- Prev by Date: Re: GNU Public Licences Revisited (again)
- Next by Date: Re: Reverse words in a string (Another Interview question)
- Previous by thread: Re: Reverse words in a string (Another Interview question)
- Next by thread: Re: Reverse words in a string (Another Interview question)
- Index(es):
Relevant Pages
|