Re: Reverse words in a string (Another Interview question)



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.

.



Relevant Pages

  • Re: Reverse words in a string (Another Interview question)
    ... >> I'm using a linked list to contain words that will be output in reverse ... A stack is exactly that. ... C's string handling sucks. ... > void reverse_string(char * ostr) ...
    (comp.programming)
  • Re: EXCEL: How to scan text reversed (like ACCESS: InStrRev)?
    ... InStrRev function. ... You could use a UDF to reverse the string, at least in later versions ... Function Reverse(str As String) As String ... you could use a UDF implementing Regular Expressions ...
    (microsoft.public.excel.worksheet.functions)
  • Re: A function of reverse complement
    ... I see how you reverse the strand. ... I do not understand how you want to complement the strand. ... Function Rev(Forw As String) As String ... >> Dim strReverseSeq As String ...
    (microsoft.public.excel.programming)
  • Re: A function of reverse complement
    ... I see how you reverse the strand. ... I do not understand how you want to complement the strand. ... Function Rev(Forw As String) As String ... >> Dim strReverseSeq As String ...
    (microsoft.public.excel.programming)
  • Re: URLDownloadToFileW Error
    ... Arvind please take advantage of tools like a debugger. ... MessageBox is cool, ... to reverse the string is beyond me. ...
    (microsoft.public.windowsce.embedded.vc)