Write a C program that accepts up to six arguments at the command line prompt wh
ID: 3824874 • Letter: W
Question
Write a C program that accepts up to six arguments at the command line prompt when you run the program. The program should print the first character of any odd- numbered arguments (numbers 0, 2, and 4), and the second character of any even-numbered arguments (numbers 1, 3, and 5). The characters printed should be separated by spaces. The program should inform the user of the correct program usage if fewer than two or more than six arguments are provided. Assume each argument contains at least two characters.
Explanation / Answer
#include <stdio.h>
int main(int argc, char *argv[])
{
int i;
if(argc < 2 || argc >6) //validate number of arguments inputted
printf(" Error: The number of command line arguments should be between 2 and 6");
else
{
printf("First character of odd numbered arguments = %c",argv[0][0]); // first argument is file name , display first character of filename
printf(" Second character of even numbered arguments = %c",argv[3]);
}
return 0;
}
Input:
Command line arguments:
myprog 0 1 2 3 4 5
Output:
First character of odd numbered arguments = m
Second character of even numbered arguments = 2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.