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

Recommended Answers

All 7 Replies

Member Avatar for iamthwee

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++.

I'm sure there is an example either in the code snippets section or on Wikipedia.

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......

commented: such attitude ...:( -2

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:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.