Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the program reverseArgs.cpp that takes command line input arguments and pr

ID: 3829694 • Letter: W

Question

Write the program reverseArgs.cpp that takes command line input arguments and prints them back in reverse order. For example, if you ran the program as ./reverseArgs first_arg second_arg, you would get an output back of second_arg first_arg.

If you ran the program as ./reverseArgs I love gorgonzola cheese, but I hate toothpaste, you would get an output back of toothpaste hate I but cheese, gorgonzola love I. This is why I (lovingly) refer to this exercise as “the Yoda program”…

If you ran the program with double quotes, then the words within should act as one unit. For example: ./reverseArgs I would rather be at "UC Santa Barbara" would output: UC Santa Barbara at be rather would I.

You are only permitted to use the iostream library.

A session should look exactly like the following example (including whitespace and formatting - note that there is no whitespace at the end of each of these lines and each printed line has a newline at the end), with all manners of different numbers for inputs and the output:

/reverseArgs /reverseArgs Hello Hello /reverseArgs Hello is it me me it is Hel reverseArgs Hello is it me "you're looking for?" you're looking for? me it is Hello reverse.Args You Are Not a Jedi! Jedi a Not Are You

Explanation / Answer

#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
for(int i = argc-1; i > 0; i--)
{
cout << argv[i] << " ";
}
cout << endl;

return 0;
}