S);
for (i=0; i
printf(“”Substring %d is “”%s””n””

Split a string by a delimiter

In this lab, you will write C code to split a given string S into substrings separated by a given character delimiter d. The substrings do not include the delimiter. Some examples are given below to illustrate how splitting is done. Suppose the delimiter d is the comma character: d = ‘,’. Then:

  • If S = “white dog”: because the delimiter d doesn’t appear in S, there is only one substring which is the same as S, so substrings = {“white dog”}.
  • If S = “white, dog”: the delimiter d = ‘,’ appears once in S, splitting S into 2 substrings, one before the comma and one after the comma, so substrings = {“white”, ” dog”} (note the leading space in the second substring).
  • If S = “,white, dog”: the delimiter d = ‘,’ appears twice in S, splitting S into 3 substrings, the first one before the first comma (which is an empty string because there is nothing before the first comma), the second substring is between the first comma and the second comma, and the third substring is after the second comma. So substrings = {“”, “white”, ” dog”} (note the first substring is empty).
  • If S = “white, dog,”: the delimiter d = ‘,’ appears twice in S, splitting S into 3 substrings, the first one before the first comma, the second substring is between the first comma and the second comma, and the third substring is after the second comma (which is an empty string because there is nothing left after the second comma). So substrings = {“white”, ” dog”, “”} (note the last substring is empty).

Use the provided code template and complete it to write a C program that does the following:

  1. It defines a function named split_string with the following prototype:

    int split_string(const char *S, const char delim, char ***substrings);
    

    This function splits a given string S by the given delimiter character delim. It allocates the memory (using malloc()) for an array of strings (of type char ** which is a pointer to an array of pointers to arrays of characters, equivalent to an array of character arrays or an array of strings). This “array of strings” contains the substrings resulting from the splitting, and is assigned to an external variable pointed to by substrings (passing by pointer). This allocated memory for the result array must later be freed (see the next function). The function split_string also returns the number of substrings, which is the number of elements allocated in substrings.

  2. It defines a function named split_string_free with the following prototype:

    void split_string_free(char **substrings, int n);
    

    This function frees all the memory allocated by split_string() for the resulting substrings which has n elements (n here is the number of substrings returned from split_string()). Important: it must free not only the memory allocated for the array but also the memory allocated for each of the substrings; otherwise there will be a memory leak.

  3. The main function and other supporting functions are already provided in the code template. Do not delete them. An example of how the functions split_string() and split_string_free() are used can be found in the implementation of the function test_split_string() in the template. The main function will read a string from the input, split it using the delimiter d = ‘|’, and print out the substrings.

Ex: If the input is:

white| dog

then the output is:

For the string "white| dog" the substrings are:
Substring 1 is "white"
Substring 2 is " dog"

#include

/* Split a given string S by the given delimiter character delim.
Returns an array of substrings from the original string, separated by the delimiter, to substrings,
which must be freed by the function split_string_free().
Return value is the number of substrings.
*/
int split_string(const char *S, const char delim, char ***substrings) {
// Your implementation here
}

/* Free all the memory allocated by split_string() for the resulting substrings. */
void split_string_free(char **substrings, int n) {
// Your implementation here
}

void test_split_string(const char *S) {
char **substrings;
int nSubs;

nSubs = split_string(S, ‘|’, &substrings);
int i;
printf(“For the string “%s”” the substrings are:n””