Write a program that processes the command line parameters according to the usag
ID: 645840 • Letter: W
Question
Write a program that processes the command line parameters according to the usage: transpose2 [-2] [-r] [-p d|x] n. This means the program can accept an optional parameter -2, followed by an optional parameter -r, followed by an optional parameter -p (which must be followed by one of the strings d or x or o), followed by an (non-negative) integer n. Valid examples include:
transpose2 0; transpose2 -2 4; transpose2 -r -p d 5; transpose2 -2 -p d 8; transpose2 -p x 16
The program should print out a `usage' message and exit with a status of 1 if the parameters are invalid. Invalid examples include:
transpose2; transpose2 -t 4; transpose2 -p b 5; transpose2 -2 -p x d; transpose2 -p x
Note that your program may treat as valid or invalid the cases where the -2, -r and -p options are repeated or out of order.
Explanation / Answer
# include
int main(int argc, char *argv[])
{ if (argv[argc] > 0)
{ /* check whether the last value of the command line is an integer */
for ( i=0; i
{ switch (argv[i]) /* use switch to check the value given is in the list provided */
{ case "-2" break;
case "-r" break;
case "-p" break;
case "d" break;
case "x" break;
} /* switch ends */
} /*for ends */
printf( " All the parameters are valid" );
return;
} /* if ends */
else
printf ( "Invalid parameters" );
return(1);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.