cs:c_language:argc_and_argv
Return to Home page
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?do=diff&rev2%255B0%255D=&rev2%255B1%255D=1551188114&difftype=sidebyside
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | cs:c_language:argc_and_argv [2024/04/08 22:35] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== 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 '' | ||
+ | |||
+ | Examples: | ||
+ | <code bash> | ||
+ | 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. | ||
+ | |||
+ | <file C 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 < | ||
+ | and print only the first < | ||
+ | */ | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #define LEN 50 | ||
+ | |||
+ | |||
+ | int main(int argc, char *argv[]) { | ||
+ | int i; | ||
+ | char s[LEN+1]; | ||
+ | int string_length; | ||
+ | |||
+ | /* Running the program as follows: | ||
+ | | ||
+ | the variables *argv[] and argc contains the following values: | ||
+ | |||
+ | argv[0]=" | ||
+ | argv[1]=" | ||
+ | argv[2]=" | ||
+ | argv[3]=" | ||
+ | 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] = ' | ||
+ | |||
+ | }else{ /* If the only argument is < | ||
+ | |||
+ | strcpy(s, argv[1]); | ||
+ | string_length = strlen(s); | ||
+ | |||
+ | } | ||
+ | |||
+ | for(i=0; i< | ||
+ | s[i] = toupper(s[i]); | ||
+ | } | ||
+ | | ||
+ | printf(" | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | **Solution 2:**\\ | ||
+ | In this second solution, all the needed controls on command line arguments have been performed. Unfortunately, | ||
+ | |||
+ | <file C 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 < | ||
+ | and print only the first < | ||
+ | */ | ||
+ | |||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | |||
+ | #define LEN 50 | ||
+ | |||
+ | |||
+ | int main(int argc, char *argv[]) | ||
+ | { | ||
+ | int i; | ||
+ | char s[LEN+1]; | ||
+ | int string_length; | ||
+ | |||
+ | /* Running the program as follows: | ||
+ | | ||
+ | the variables *argv[] and argc contains the following values: | ||
+ | |||
+ | argv[0]=" | ||
+ | argv[1]=" | ||
+ | argv[2]=" | ||
+ | argv[3]=" | ||
+ | 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(" | ||
+ | printf(" | ||
+ | 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], | ||
+ | printf(" | ||
+ | exit(-2); | ||
+ | } | ||
+ | |||
+ | /* Control if the second arguments is a number */ | ||
+ | for(i=0; i< | ||
+ | if(isdigit(argv[2][i])==0){ | ||
+ | printf(" | ||
+ | exit(-3); | ||
+ | } | ||
+ | } | ||
+ | string_length = atoi(argv[2]); | ||
+ | /* string_length = atoi(argv[2]); | ||
+ | /* sscanf(argv[2], | ||
+ | printf(" | ||
+ | |||
+ | /* Control if the number (i.e., the variable string_length) is positive */ | ||
+ | if(string_length < 0){ | ||
+ | printf(" | ||
+ | exit(-4); | ||
+ | } | ||
+ | /* If the actual string is shorter then string_length, | ||
+ | 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])> | ||
+ | printf(" | ||
+ | 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] = ' | ||
+ | |||
+ | }else{ /* If the only argument is < | ||
+ | |||
+ | /* Control if the string contained in argv[3] can be stored in s */ | ||
+ | if(strlen(argv[1])> | ||
+ | printf(" | ||
+ | exit(-5) ; | ||
+ | } | ||
+ | |||
+ | /* Store the string */ | ||
+ | strcpy(s, argv[1]); | ||
+ | string_length = strlen(s); | ||
+ | } | ||
+ | |||
+ | for(i=0; i< | ||
+ | if(!isprint(s[i])){ | ||
+ | printf(" | ||
+ | exit(-6); | ||
+ | } | ||
+ | | ||
+ | s[i] = toupper(s[i]); | ||
+ | } | ||
+ | | ||
+ | printf(" | ||
+ | |||
+ | 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?do=diff&rev2%255B0%255D=&rev2%255B1%255D=1551188114&difftype=sidebyside
/web/htdocs/www.skenz.it/home/data/pages/cs/c_language/argc_and_argv.txt · Last modified: 2024/04/08 22:35 by 127.0.0.1