Algorithm to reverse order of words in a string