parameter string problems

hi can anyone show me how to set this code i have below, to reverse all parameters i set, instead of just reversing my string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

int main (int argc, char *argv[])
{
    
    
  string str ("hello world");
  string::reverse_iterator rit;
  for ( rit=str.rbegin() ; rit < str.rend(); rit++ )
    cout << *rit;
    
system("pause");
  return 0;
}



also i will have to reverse my parameters by word, i.e parameter "hello world" reversed to "world hello" if anyone could recomend some reading material that would be greatly appreciated.
Last edited on
also i will have to reverse my parameters by word, i.e parameter "hello world" reversed to "world hello" if anyone could recomend some reading material that would be greatly appreciated.

What you'll need is to divide your string into sub strings, knowing that a new string must start after a space.

for example:

str("hello world") should be transformed to string[2]={"hello","world"}

There are a few ways of doing this. You can transform your string to a c-style string and use strtok() you use a stringstream to hold your string and use getlint(str, separate_word, " ") or use a boost library.

google: "stl string tokenizer" and you'll find many people with the same problem and many solutions for it.

If you still can't make it, came back again, gl ;)
ok i had a look and came close, but i still can't figure it out, can a get a hand please?
The simplest way of achieving what you want is probably using stringstream.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
    string s("This is a string I want to break apart.");
    istringstream iss(s);

    string buf;

    while(getline(iss, buf, ' '))
        {
            cout<<buf<<endl;
        }
}


This works by transforming s to a stream. We can then use the getline method on iss.

As iss is now a stream getline will work by reading a string up until your delim char is found, leaving in the stream the unread part of the string.

All you have to do know is to save each word in a vector or array and print them in reverse order.
thanks that helped alot.
Topic archived. No new replies allowed.