-
You can start your coding by copying and pasting the first sample that inputs
multi-line text from the user. Make sure that the program compiles and runs
properly.
-
Add the following code to print the text on the screen:
std::copy(
vect.begin(),
vect.end(),
std::ostream_iterator< std::string >( std::cout, "\n" )
);
-
For this modified code to compile properly, you must also add the library
header that provides STL algorithms:
#include <algorithm>
-
Again, make sure that the program compiles and runs properly at this point.
-
Add code to access individual characters of the command-line argument from
another sample above:
for ( int idx = 0; argv[1][idx]; ++idx ) {
std::cout << argv[1][idx] << std::endl;
}
Instead of printing characters on the screen, use a switch statement,
or a series of if-else statements to examine
individual characters. You are now ready to write code to handle each possible
case: 'S', 'R', and 'U', as well as their lowercase equivalents, 's', 'r', and
'u'.
-
Use the following STL algorithm to implement sort :
std::sort(
vect.begin(),
vect.end()
);
-
Use the following STL algorithm to implement reverse :
std::reverse(
vect.begin(),
vect.end()
);
-
Elimination of non-unique lines from the input is a bit trickier. Since some
lines will be removed from the original input, the resulting text will become
shorter. Because of this, the std::unique algorithm of the C++
Standard library returns the new end of the container that is being
processed by the program. That new end must be used when the program displays
the final output. Here is an example:
std::vector< std::string >::iterator new_end = std::unique(
vect.begin(),
vect.end()
);
std::copy(
vect.begin(),
new_end, // in all other cases: vect.end(),
std::ostream_iterator< std::string >( std::cout, "\n" )
);
While this is not a big challenge, the program should distinguish "uniquify"
from all other cases, where the entire vector is printed. Since the program
does its work and exits, your code could have the proper way of displaying the
output for each algorithm and use a return statement to
exit out of the program.
-
Finally, you can improve your program by adding some logic to handle a
situation when no argument on the command line is present. In such case,
program should default to 'U', that is, assume that the user wants to uniquify
the input.
|