/* 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 ' between the name of the program and the string is used to convert and print only the first characters of the string. */ #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: 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 )? \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[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: 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 */ /* Control if the string contained in argv[3] can be stored in s */ if(strlen(argv[1])>LEN){ printf("Error: too long\n") ; exit(-5) ; } /* Store the string */ strcpy(s, argv[1]); string_length = strlen(s); } for(i=0; i