Reserving a word

Lets say I have a string "THIS IS GREAT !!" I need to reverse the phrase but only the ORDER of the words. The output would be "!! GREAT IS THIS".

Can anybody help me out with a loop that would do this?

Thanks to those who help!
Write a function char* find_word(char* start) where start is (a pointer to) the first char of the string and the return value is (a pointer to) the last one (the first whitespace). When *start == 0, return start.

Now put this function in a loop where, while the returned value is not equal to the argument, add the string between the argument and return value to the result string (starting from its end).

Of course, you don't really need a separate function, but I suppose it is easier to think when it is.

I think it would be slightly simpler to implement if find_word's argument would be the end of a word and it would find the beginning.
If you don't mind, could you please write the loop for me?
I do. That wouldn't be of much use to anyone. You'll learn more if you write it yourself. What problems are you having? Approach the problem step by step. Are you able to write the find_word function?
Another approach to the problem (hamsterman has a good one too) is to take each "word" (series of non-space chars) and put it into one element of an array or vector (vector is better if you don't know how many words there will be in the line) and then just output each element starting from the end backwards.
EDIT: forgot to specify: a vector of strings
Last edited on
I like the vector approach. But I have no idea how to write the vector. Can you please write one for me?
Didn't test it so it might have mistakes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
std::vector<std::string> vWords;
std::string str = "THIS IS GREAT !!";
while(1)
{
    std::string temp;
    unsigned int pos = str.find_first_of(' ');
    if(-1 == pos)
        break;
    temp = str.substr(0, pos);
    vWords.push_back(temp);
    str.erase(0, pos + 1);
}
...
//print in reverse order 


printing
1
2
3
4
5
6
std::vector<std::string>::reverse_iterator rb(vWords.begin());
std::vector<std::string>::reverse_iterator re(vWords.end());
while(re < rb)
{
	std::cout << *re++ << " ";
}
Last edited on
This is what I have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>
#include <vector>

using namespace std;

void main ()
{
vector<string> vWords;
string str = "THIS IS GREAT !!";
while(1)
{
    string temp;
    unsigned int pos = str.find_first_of(' ');
    if(-1 == pos)
        break;
    temp = str.substr(0, pos);
    vWords.push_back(temp);
    str.erase(0, pos + 1);
}

}

void Display (string str)

{
	cout << "Backwards: " << str << endl;
}


...and the output is nothing.


:(
Well, the main problem is probably that you never cout<< anything.
Zhuge: I cout the the string in void Display.
Never mind I took that function out.

Here is the code I have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
#include <vector>

using namespace std;

void main ()
{

string str;

vector<string> vWords;
cout << "Please enter a phrase" << endl;
getline(cin, str);
while(1)
{
    string temp;
    unsigned int pos = str.find_first_of(' ');
    if(-1 == pos)
        break;
    temp = str.substr(0, pos);
    vWords.push_back(temp);
    str.erase(0, pos + 1);
}
cout << "Backwards: " << str << endl;
}


The output is the entered phrase, it doesn't change at all.
You make no use of what you pushed into vWords. savavampir posted the code for that.
Topic archived. No new replies allowed.