Write a C program that accepts up to six arguments at the command line prompt. The program should print the first character of any odd numbered arguments, and the last character of any even numbered arguments. 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. For example
Should print this:
Given arguments: myprog arg1 200 list all arg5
Returns: m 1 2 t a 5
How would I get my code to list the arguments inputted by name as well as the last character of even numbered arguments?
#include<stdio.h>
int main(int argc,char* argv[])
{
int counter;
//here in c always one argument is ./a.out so we will check counting one extra
//argc contains the number of argument passed
//argv contain all the arguments
if(argc<3 || argc>7)
{
//invalid number of arguments
printf("\nplease pass appropriate number of command line attributes");
}
else
{
//printing all the arguments
printf("\nName of arguements passed: %s\n", argv[counter]);
//looping through all argumnets
for(counter=0;counter<argc;counter )
{ //if is at even place but here odd as numbering from 1
if(counter%-1==0){
printf("%c ",argv[counter][0]);
}
//odd place
else
{
printf("%c ",argv[counter][0]);
}
}
}
return 0;
}
my code is printing this:
name Of Arguments Passed: myprog
. m a 2 l a a
CodePudding user response:
You need to use the function strlen(<str>)
to calculate the length of the string to get the last letter (make sure to -1 from the result). The strlen
function is from <string.h>
Quick note 0:
As you set counter
to 0 in your for loop you actually include a.out
as an argument (change it to counter=1
) and change (counter%-1==0)
to (counter%2==1
)
Quick note 1:
you can clean up your code by exiting if an invalid number of args is passed removing an indent level
if(argc<3 || argc>7)
{
//invalid number of arguments
printf("\nplease pass appropriate number of command line attributes");
return 1; // 1 means an error occured
}
// rest of code here
Quick note 2:
You declare counter
but do not initialize it meaning printf("\nName of arguements passed: %s\n", argv[counter]);
has undefined behaviour as counter
could be anything. To fix this either change the declaration to counter = 0
or change argv[counter]
to argv[0]
. Otherwise, the code will sometimes cause a segfault.
Quick note 3 (sorry):
'\n' should be placed at the end of print statements not at the beginning.
By placing them at the beginning you make the output very weirdly formatted
> ./a.out myprog arg1 200 list all arg5
Name of arguements passed: ./a.out
. m a 2 l a a 13 >
Final code:
#include<stdio.h>
#include<string.h>
int main(int argc,char* argv[])
{
int counter = 0;
//here in c always one argument is ./a.out so we will check counting one extra
//argc contains the number of argument passed
//argv contain all the arguments
if(argc<3 || argc>7)
{
//invalid number of arguments
printf("please pass appropriate number of command line attributes\n"); // new line is added at the end
return 1;
}
//printing all the arguments
printf("Name of arguements passed: %s\n", argv[counter]); // new line is added at the end
//looping through all argumnets
for(counter=1;counter<argc;counter )
{ //if is at even place but here odd as numbering from 1
if(counter%2==1){ // used to be (counter%-1==0)
printf("%c ",argv[counter][0]);
}
//odd place
else
{
printf("%c ",argv[counter][strlen(argv[counter]) - 1]);
}
}
putchar('\n'); // add new line to the end
return 0;
}
CodePudding user response:
This for loop
for(counter=0;counter<argc;counter )
{ //if is at even place but here odd as numbering from 1
if(counter%-1==0){
printf("%c ",argv[counter][0]);
}
//odd place
else
{
printf("%c ",argv[counter][0]);
}
}
is incorrect at least because the initial value of the variable counter should be equal to 1. argv[0]
is not a user supplied argument.
Also the condition in the if statement
if(counter%-1==0){
does not make a sense. It seems there is a typo.
The loop can look the following way
#include <string.h>
//...
for ( counter = 1; counter < argc; counter )
{
if ( counter % 2 != 0 )
{
printf( "%c ", argv[counter][0] );
}
else
{
printf( "%c ", argv[counter][strlen( argv[counter] ) - 1] );
}
}
Though it will be safer to write the loop like
for ( counter = 1; counter < argc; counter )
{
if ( *argv[counter] )
{
if ( counter % 2 != 0 )
{
printf( "%c ", argv[counter][0] );
}
else
{
printf( "%c ", argv[counter][strlen( argv[counter] ) - 1] );
}
}
}
Because the user can specify an empty string "" as a command line argument.