In: Computer Science
Write a C++ program that prints out all of the command line
arguments passed to the program.
Each command line argument should be separated from the others with
a comma and a space.
If a command line argument ends in a comma, then another comma
should NOT be added
C++ Program:
/* C++ program that prints out all of the command line arguments passed to the program */
#include <iostream>
#include <string>
using namespace std;
//Main function
int main(int argc, char* argv[])
{
int i, len;
//Printing total number of command line
arguments
cout << "\n\n Total number of Command line
arguments : " << argc << "\n\n";
cout << "\n Command line arguments: \n\n";
//Iterating over command line arguments
for(i=1;i<argc;i++)
{
//Getting length of
current argument
len =
strlen(argv[i]);
//If last character
is a comma
if(argv[i][len-1] ==
',')
{
//Just print the argument along with space
cout << argv[i] << " ";
}
else
{
//Just print the argument along with comma and space
cout << argv[i] << ", ";
}
}
cout << "\n\n";
return 0;
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Sample Output: