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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
// This program only calls the other functions written by you.
// It allows you to test their output.
// Do not modify it in any way.
// Compile this file only: $ gg -o driver driver.cpp
// The #include directives will include your definitions
// appropriately.
#include <iostream> // std::cin, std::getline(), std::cout, std::endl;
#include <string> // std::string
#include <cstring> // strcmp()
#include "strip.cpp" // test_strip()
#include "pal.cpp" // test_is_palindrome()
void display_usage()
{
using std::cout;
using std::endl;
cout << "Usage: driver strip [sentence]" << endl;
cout << " or: driver pal [sentence]" << endl;
cout << "The [sentence] argument is optional, but you" << endl;
cout << "will be prompted for it if you omit it." << endl;
return;
}
std::string get_sentence(int argc, char *argv[])
{
std::string result;
if (argc == 2)
{
std::cout << "Enter a sentence: ";
std::getline(std::cin, result);
}
else
{
for (int i = 2; i < argc - 1; ++i) // argc is an int, not a size_t
{
result += argv[i];
result += " ";
}
result += argv[argc - 1];
}
return result;
}
int main(int argc, char *argv[])
{
if (argc == 1 or (strcmp(argv[1], "strip") != 0 and strcmp(argv[1], "pal") != 0))
{
display_usage();
}
else
{
std::string sentence = get_sentence(argc, argv);
if (strcmp(argv[1], "strip") == 0)
{
std::cout << "You entered : " << sentence << std::endl;
std::cout << "After stripping: " << strip(sentence) << std::endl;
}
else if (strcmp(argv[1], "pal") == 0)
{
std::cout << "You entered: \"" << sentence;
std::cout << "\", which ";
std::string is_not = (is_palindrome(sentence)) ? "is" : "is not";
std::cout << is_not;
std::cout << " a palindrome." << std::endl;
}
}
return 0;
}
|