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