Using C For this quiz you must write an implementation of th

Using C.

For this quiz, you must write an implementation of these two functions: The function add string accepts an array of zero or more strings that has a NULL in the last location. It creates a shallow copy of the array that is one location larger, appends a copy of the string add onto the array. It then deletes the original array and returns the new copy This is one way to add things to a dynamically allocated array. Every time we add something, we make a new version that is one location larger and destroy the original. Here is how this is used in a program: char **array = NULL; char **traverse;//An initial, array with no strings in it. array = (char **)calloc (1, sizeof (char *)); array = add string (array, \"bird\"); array = add-string (array, \"dog\"); array = add-string(array \"cat\"); array = add string (array, \"llama\"); array = add-string (array, \"mule\"); array = add-string(array, \"fish\"); for (traverse = array; *traverse traverse ++) {printf (\"%s\ \", *traverse);} free-strings (array); Each time we call add string we are creating a new memory allocation that is our array. That is why we both pass array to it and set array from the return value The function free strings() frees all memory allocated to the array of strings Write your solution in the window below labeled Code. At any time you can press the Test button below to send your solution to a remote system for testing. The compilation and testing results will appear in the second window below labeled Result. When you have completed the quiz, press Submit to submit your solution. Be sure to also press Finish Quiz to get credit for the quiz Here is a complete starter file including the header file you will need to include. stringarray.h does include both string.h and stdio.h so you do not have to include those. ainclude \"stringarray.h\" char ** add string(char **existing, const char *add)} {void free-strings (char **strings) {} The program quiz system is persistent, meaning your program will remain in the window below until the assignment closes

Solution

char **add_string(char **existing, const char *add){
while(*(strcpy++)!=\'\\0\');
strcpy(*strcpy,*add);
strcpy = (char **)calloc(1,sizeof(char *));
strcpy(*(++strcpy),\'\\0\');
return strcpy,
}

void free_strings(char **strings){
while (*strings)
{
free(*strings);
(*strings)++;
}
}

Using C. For this quiz, you must write an implementation of these two functions: The function add string accepts an array of zero or more strings that has a NUL

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site