Hi!! can anybody gimme an algo for reversing the words in a string in O(n) time....
Eg..i/p:I love Linux
o/p: Linux love I

Hi!! can anybody gimme an algo for reversing the words in a string in O(n) time....
Eg..i/p:I love Linux
o/p: Linux love I

Hi, which language do you want this for? c or c++.

Hi!! can anybody gimme an algo for reversing the words in a string in O(n) time....
Eg..i/p:I love Linux
o/p: Linux love I

Do you mean reversing the string or reversing the order of words? If it's the string, then you'd get:

I love Linux -> xuniL evol I

versus the sample you gave for reversing word order.

>can anybody gimme an algo for reversing the words in a string in O(n) time....
Recursively copy the words to a new string. ;)

Doesn't matter which lang it is...as long as i get the algo......Anyway i have clearly xplained what o/p i shud get......

Comments
such attitude ...:(

Out of curiosity, did you see the announcement thread titled "We only give homework help to those who show effort?" What have you tried, what do you think is wrong with it?

Doesn't matter which lang it is...as long as i get the algo......

Alright, if it doesn't matter which language it's in, I'll give it to you in Pseduocode...

reverse words function:

1. remove first word from sentance
2. call myself again with the remaining part of the string
3. add the first word to the last part of the sentance
4. return the sentance

It might be a bit hard finding a compiler for this code, though... :mrgreen:

This article has been dead for over six months. Start a new discussion instead.