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

can anyone do it asap PROGRAM DESCRIPTION: The goal of this recitation assignmen

ID: 3746978 • Letter: C

Question

can anyone do it asap

PROGRAM DESCRIPTION: The goal of this recitation assignment is to get you more failiar with parsing command- line arguments through some practice with this concept. In this recitation assignment, you will write a short, but complete C program that will accept any arbitrary number of options (as indicated by a single in front of a single letter character) along with a filename. Specifically, your program will be invoked using the following format: /executable options, each preceded by - filename This means that your program name will be followed by an arbitrary number of options, where each option will be identified by a followed by a single letter. The last commandHine argument will always be a filename (whether the file actually exists or not is not important). The goal of your program is to simply parse the entire command line and print out arn itemized list of all of the arguments according to the specified requirements: - At a minimum, the user must type the command name and a filename (as indicated by the program format expected). If the user does not enter at least a filename, then you will print a usage statement and terminate the program. Print a label for each argument component (i.e., "command", "option", or "filename"), where each option is numbered. . For the command name, you will strip off the first two characters (i.e., the "./") so that it prints just the actual command name without the leading current path specification. Hint: Remember that each argument is just a character array. * For each option identified by the leading , you will print out the single letter character for each option argument (note that the command may be executed with no options with only a filename). . For the filename, you will simply print out the filename as given. For this exercise, you may assume that the user enters the arguments properly formatted and in order, so no real error checking is needed other than the minimum number of command-line arguments expected.

Explanation / Answer

/*******************************************************************************/

#include <stdio.h>
#include<string.h>

int main(int argc, char **argv)
{
int i,j;
char str[20];
  
if(argc < 2)
{
printf("Usage: ./a.out [- options ] filename ");
return -1;
}
//access each options and command
printf("Command :%s ",argv[0]);
for(i = 1; i < argc ; i++)
{
//check first option if first character is '-', its option , otherwise its' command
if(argv[i][0] == '-')
{
//copy option leaving '-' character
for(j = 0; j < strlen(argv[i])-1 ; j++)
{
str[j] = argv[i][j+1];
}
str[j] = '';
printf("Option %d:%s ",i,str);
}
else
{
printf("File name: %s ",argv[i]);
}
}

return 0;
}

-----------------------------------------------------------------------------

//output1
//if you run
./a.out
Usage: ./a.out [- options ] filename , you get
//output2
if you run as below
/home/a.out filename
output is
Command :/home/a.out   
File name: filename
//output 3
IF you run with options and commands
/home/a.out -t -r -s -p -w somefile
Command :/home/a.out   
Option 1:t   
Option 2:r   
Option 3:s   
Option 4:p   
Option 5:w   
File name: somefile

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote