Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi,


I need to reverse the string "word by word" .

Reverse the string word by word, in place.

for example
if our string is “the house is blue”,
the return value would be “blue is house the”.

The words are reversed, but the letters are still in order (within the word).

Now the problem is how could it be done?

Thanks
Posted
Updated 22-Dec-18 4:26am
Comments
ThatsAlok 4-Jul-11 3:31am    
seems to homework, however you can use AfxExtractSubString to get unique string from string

Here's one concept that requires a minimum of temporary storing, although it's hopelessly inefficient (if you wanted to be efficient you wouldn't try in place reversal to start with!):

1. Start by writing a function that swaps two neighbouring words in a string. Watch out for cases where the target location of a copy overlaps the source!
2. Continue with a function that implements swaping two arbitrary words as a series of neighbouring word swaps.
3. Create a function that determines the pairs of words that you need to swap.
4. Then write a function that does the required reversal in terms of individual word swaps.
 
Share this answer
 
Comments
Emilio Garavaglia 4-Jul-11 8:18am    
Not efficient, but elegant!
Sergey Alexandrovich Kryukov 5-Jul-11 4:31am    
Sorry, I don't think it's elegant either. You simply split the source by ' ', than regenerate string by in-reverse iteration.
--SA
Stefan_Lang 5-Jul-11 4:43am    
'regenerate' is not 'in-place', which is a requirement of the task.

My suggestion only requires you to temporarily store one word at a time while shifting other words around, right within the original array. I'm not saying this is efficient - but if that were a requirement, then 'in-place' reordering of a string is definitely the wrong way to start.
Sergey Alexandrovich Kryukov 5-Jul-11 5:06am    
Oh... I see. Does it means my permutation of the same string? Then it's never strictly in place as you need a buffer for temporary fragment. Still I don't see benefits of you solution, sorry. I would do it better, I think. How about algorithm:
1) Start by writing a function which move leftmost work to the very right
2)... and so on.
Preliminary count the number of words....
--SA
Stefan_Lang 5-Jul-11 5:42am    
According to http://en.wikipedia.org/wiki/In-place_algorithm an in-place algorithm does two things:
1. overwrite the input with the output
2. Use no more than O(1) additional memory for temporary and helper variables.

The latter invalidates, strictly speaking, all algorithms that store a whole word from that sentence, because we cannot easily determine a constant size that is sufficient to store any word. (we may be able to do so with a few assumptions about the language being used and the validity of compound words, but there are languages, such as german, that allow the construction of compound words of virtually unlimited size!)
You got lots of links to C or C like solutions, this is a working C++ one, certainly not fit to your homework, maybe helpful later:
C++
// ReverseWords.cpp : Defines the entry point for the console application.

#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>

using namespace std;

void ReverseWords(char* str)
{
    vector<string> words;
    istringstream iss(str);
    copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter(words));
    ostringstream oss;
    copy(words.rbegin(), words.rend(), ostream_iterator<string>(oss, " "));
    copy_n(&oss.str().front(), oss.str().length() - 1, str);
}

int main()
{
    char str[] = "the house is blue";
    cout << str << endl;
    ReverseWords(str);
    cout << str << endl;
    return 0;
}

cheers,
AR
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Jul-11 4:33am    
Looks not bad, my 5.
--SA
Stefan_Lang 5-Jul-11 5:49am    
Good algorithm, but not in-place. See http://en.wikipedia.org/wiki/In-place_algorithm
We're doing your homework for you, we're helping people. You're really interested in doing your homework by yourself. You're given a change to develop some skills, don't loose it, it will pay off in near future.

If you get stuck and can formulate your problem clearly and provide a small and self-consistent code sample demonstrating your problem we will gladly help you.

Good luck,
—SA
 
Share this answer
 
Comments
Awa_tcp 4-Jul-11 2:56am    
u guys only commenting not helping ..have u given any right answer..
ShilpiP 4-Jul-11 3:10am    
How can you talk like that? No one is here to do your work. You have to come with your code and with specific problem.
ShilpiP 4-Jul-11 3:17am    
1) http://www.codeguru.com/forum/showthread.php?t=303185
2) http://www.cplusplus.com/forum/general/2876/
3) http://anaturb.net/C/string_exapm.htm
4) http://www.cplusplus.happycodings.com/Algorithms/code26.html
Sergey Alexandrovich Kryukov 5-Jul-11 0:45am    
Can I up-vote this? :-)
As to you note above, please see comments below... :-)
--SA
ShilpiP 5-Jul-11 1:16am    
No ... I just want to realize that guy that Google search will give n number of answer ...
I think you didn't see my above comment.
"How can you talk like that? No one is here to do your work. You have to come with your code and with specific problem."
It's easy. It's obviously homework, so I wont do it ( but I'm sure some moron who cares more about getting your vote than helping you learn, will ). You know how to traverse a string in a loop, right ? You know you can look at each character by index ? If you search and you find spaces, then you've found each word, and can copy that word in to your new string. Now, go for it !!!
 
Share this answer
 
Comments
Christian Graus 4-Jul-11 2:56am    
The only way to do it in place, is to work out your letter positions before hand and then start swapping characters in the manner I described.
This is a very standard homework question.
However, see this[^]. Hope it guides you.
 
Share this answer
 
Comments
Stefan_Lang 5-Jul-11 5:52am    
Unfortunately that solution reverses words, rather than the sequence of the words in a string, as required.
seems to homework, however you can use AfxExtractSubString to get unique string from string, rest i don't think would be tough for you!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900