Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help me to finish my C Programming... Step 1: print all command-line argu

ID: 3874242 • Letter: P

Question

Please help me to finish my C Programming...

Step 1: print all command-line arguments

If a user enters a command-line arguments such as: “God loves you!” For example:
$> struct.exe God loves you!
$> c:usersuserDropboxCodeBlockstructstruct.exe $> God
$> loves
$> you!
$>

Step 2 ? invoke argscat() with argc and argv

1. First of all, define a variable to store a delimiter.
2. Define a function prototype to access the argscat() in main.c.
- char *argscat(int argc, char *argv[], char *delimiter);
3. Then create a file called argscat.c to implement the argscat() function.
- Use assert() after malloc() to check the return status of malloc() call.
4. Invoke argscat() with argc and argv in main.c.
5. Use assert() function.
6. Debug the code if necessary. You may run argscat.c provided.

Step 3 – invoke argscat() such that it excludes the first argument

If argscat() is properly implemented, you don’t need to change it in this step. You just modify how to invoke the function itself.

Step 4 – use a pointer to print the args string in uppercase

- Use the pointer to string you got in step 3 here. - Print it in uppercase excluding non-alpha using a while loop.
- Use toupper() to convert and isalpha() built-in functions.
- Don't make a copy of the string.
- Don’t convert the string itself to an uppercase string, but use a pointer and pointer arithmetic to access each character.

Step 5 – use an array indexing to print the args string in lowercase

- Use the pointer to string you got in step 3 here. - Print it in lowercase excluding non-alpha using a for loop.
- Use tolower() to convert and isalpha() built-in functions.
- Don't make a copy of the string.
- Don’t convert the string itself to a lowercase string, but use an array indexing to access each character.

Step 6 – modify main() in main.c to free resources allocated in argscat()

Add some code in main() to free the memory allocated in argscat().
Since there were two calls of malloc(), two calls of free() are expected to free all resources allocated. Remember that the number of malloc() calls should match with that of free() calls.


(this is a skeleton file)

/**
* Week3: Homework 1 - Implement argscat() function
*
* argscat() accepts an array of pointers to strings and
* returns a concatenated string separated by a delimiter
* passed by the user.
*/

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    printf("Happy Coding! ");

    // step 1: print all command-line arguments
    for (int i = 0; i < argc; i++)
        printf("%s ", argv[i]);

    // step 2: invoke argscat() with argc and argv.


    // step 3: invoke argscat() such that it excludes the first argument.


    // step 4: Use the pointer to string you got in step 3 here.
    // print it in uppercase excluding non-alpha using a while loop.
    // don't copy the string, but use a pointer and pointer arithmetic


    // step 5: Use the pointer to string you got in step 3 here.
    // print it in lowercase excluding non-alpha using a for loop.
    // don't copy the string, but use a pointer and pointer arithmetic


    return 0;
}

Explanation / Answer

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

char *argscat(int argc, char *argv[], char *delimiter) {
int i=0, totalLength = 0;
for(i=0; i<argc; i++) {
totalLength += strlen(argv[i]);
}
// between 5 words, there will be 4 spaces, and 1 null character to terminate the string
totalLength += argc;
  
if(argc == 0) {
totalLength = 1;
}
  
char *output = (char *)malloc(totalLength * sizeof(char));
assert(output != NULL);
  
int count = 0, j=0;
for(i=0; i<argc; i++) {
for(j=0; j<strlen(argv[i]); j++) {
output[count++] = argv[i][j];
}
output[count++] = *delimiter;
}
output[count] = '';
return output;
}

int main(int argc, char *argv[]) {
printf("Happy Coding! ");
  
char delimiter = '-';
  
// step 1: print all command-line arguments
for (int i = 0; i < argc; i++)
printf("%s ", argv[i]);
  

// step 2: invoke argscat() with argc and argv.
char *result1 = argscat(argc, argv, &delimiter);
  
// step 3: invoke argscat() such that it excludes the first argument.
char *result2 = argscat(argc-1, argv+1, &delimiter);
  
// step 4: Use the pointer to string you got in step 3 here.
// print it in uppercase excluding non-alpha using a while loop.
// don't copy the string, but use a pointer and pointer arithmetic
int i=0;
while(*(result2+i) != '') {
if(isalpha(result2[i])) {
printf("%c", toupper(result2[i]));
}
i++;
}
printf(" ");
  
// step 5: Use the pointer to string you got in step 3 here.
// print it in lowercase excluding non-alpha using a for loop.
// don't copy the string, but use a pointer and pointer arithmetic
i=0;
while(*(result2+i) != '') {
if(isalpha(result2[i])) {
printf("%c", tolower(result2[i]));
}
i++;
}
  
// free the malloc generate memory
free(result1);
free(result2);
  

return 0;
  
}

============================================

Please save the above code in a .c file and run. Thanks!