Argc and Argv (Example 1)
Concepts:
Command line arguments.
Text:
Implement a C program that receives through the command line a string as input. The program must transform the content of the string in uppercase letters and print it on the screen.
The option -c <number>
between the name of the program and the string is used to convert and print only the first <number>
characters of the string.
Examples:
C:> prog_name.exe a_string_123 A_STRING_123 C:> prog_name.exe -c 4 a_string_123 A_ST
Soluzions:
Two solutions will be provided.
Solution 1:
In this first solution, less complex, no controls have been performed on command line parameters. The hypotesis is that the user of the program use it in a correct way.
- argc_argv_1a.c
/* Implement a C program that receives through the command line a string as input. The program must transform the content of the string in uppercase letters and print it on the screen. The option '-c <number>' between the name of the program and the string is used to convert and print only the first <number> characters of the string. */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #define LEN 50 int main(int argc, char *argv[]) { int i; char s[LEN+1]; int string_length; /* Running the program as follows: prog_name.exe -c 4 sTriNg the variables *argv[] and argc contains the following values: argv[0]="prog_name.exe" argv[1]="-c" argv[2]="4" argv[3]="sTriNg" argc=4 And the output of the program will be STRI */ if(argc==4){ /* If the user has specified the option -c */ string_length = atoi(argv[2]); strncpy(s, argv[3], string_length); s[string_length] = '\0'; }else{ /* If the only argument is <string> */ strcpy(s, argv[1]); string_length = strlen(s); } for(i=0; i<string_length; i++){ s[i] = toupper(s[i]); } printf("The resulting string is: %s\n", s); return 0; }
Solution 2:
In this second solution, all the needed controls on command line arguments have been performed. Unfortunately, at the expense of the program readability.
- argc_argv_1b.c
/* Implement a C program that receives through the command line a string as input. The program must transform the content of the string in uppercase letters and print it on the screen. The option '-c <number>' between the name of the program and the string is used to convert and print only the first <number> characters of the string. */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #define LEN 50 int main(int argc, char *argv[]) { int i; char s[LEN+1]; int string_length; /* Running the program as follows: prog_name.exe -c 4 sTriNg the variables *argv[] and argc contains the following values: argv[0]="prog_name.exe" argv[1]="-c" argv[2]="4" argv[3]="sTriNg" argc=4 And the output of the program will be STRI */ /* Check on the number of command line arguments */ if(argc!=2 && argc!=4) { printf("Error: the program must be executed with the following arguments\n"); printf("%s (-c <number>)? <string>\n", argv[0]); exit(-1) ; /* As the return 1; command, but with exit() the program is killed also if executed inside a function */ } if(argc==4) { /* If the user has specified the option -c */ /* Control if argv[1] contains -c */ if(strcmp(argv[1], "-c")!=0){ printf("Error, the only available option is -c\n"); exit(-2); } /* Control if the second arguments is a number */ for(i=0; i<strlen(argv[2]); i++){ if(isdigit(argv[2][i])==0){ printf("Error: the argument of the -c option must be a number\n"); exit(-3); } } string_length = atoi(argv[2]); /* string_length = atoi(argv[2]); can be substituted with the command */ /* sscanf(argv[2], "%d", &string_length); */ printf("STRING LENGTH: %d\n", string_length); /* Control if the number (i.e., the variable string_length) is positive */ if(string_length < 0){ printf("The number of the -c option must be positive or equal to 0\n"); exit(-4); } /* If the actual string is shorter then string_length, set the value of string_length of the actual length of the string */ if (string_length > strlen(argv[3])) string_length = strlen(argv[3]); /* Control if the string contained in argv[3] can be stored in s */ if(strlen(argv[3])>LEN){ printf("Error: <string> too long\n") ; exit(-5); } /* The string has been stored inside the array s (a better solution can exploit directly argv[3]) */ strncpy(s, argv[3], string_length); s[string_length] = '\0'; }else{ /* If the only argument is <string> */ /* Control if the string contained in argv[3] can be stored in s */ if(strlen(argv[1])>LEN){ printf("Error: <string> too long\n") ; exit(-5) ; } /* Store the string */ strcpy(s, argv[1]); string_length = strlen(s); } for(i=0; i<string_length; i++){ if(!isprint(s[i])){ printf("The string is not valid. Some characters are not printable\n"); exit(-6); } s[i] = toupper(s[i]); } printf("The resulting string is: %s\n", s); return 0; }
If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at] skenz.it
You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url: https://www.skenz.it/cs/c_language/argc_and_argv