randomly reverse two selected words

I need to write a code which a class named String defined with data member sentence(char) which has two member functions:
1)to read the sentence (input)
2)to randomly select two words from the sentence and reverse them

eg if i give an input as "my name is komrad"
possible output can be: eman darmok

I have no clue how to go about this...please help

Thanks in advance.
At First, tokenize(decompose ) sentence into words
You can use
string::find_first_of
for it.
After that , use STL functions.
First use random_shuffle for randomly shuffling words. Or you can generate 2 random numbers by using rand() function.

Later , reverse them with STL function reverse

Last edited on
Thank you Dufrense for your prompt reply. I am a beginner in C++. could you kindly provide a sample code as to how to implement this?
Do you mean something like this:
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
29
30
31
class String
{
    // data members
    char * sentence;
public:
    // default constructor
    String() {
        /* TODO */
    }
    // copy constructor
    String( const String & s ) {
        /* TODO */
    }
    // assignment operator
    String & operator=( const String & s ) {
        /* TODO */
    }
    // destructor
    ~String() {
        /* TODO */
    }

    // method to read in a sentence
    void input() {
        /* TODO */
    }
    // method to select two words from sentence, reverse, and output them
    void output() const {
        /* TODO */
    }
};


That should get you started, if I understood the question correctly.
Last edited on
Not meaning to be snotty, but if you actually have no idea how to go about this, you should probably start with something less ambitious.
Topic archived. No new replies allowed.