/** * split.h * Split strings into component parts. */ #ifndef __SPLIT_H__ #define __SPLIT_H__ /** * Procedure: * split * Parameters: * str, a string * sep, a string * parts[], an array * n, a non-negative integer * Purpose: * Split str into up to n separate parts. * Produces: * [Nothing; Called for the side effect] * Preconditions: * n <= size of parts * str != NULL * sep != NULL * Postconditions: * parts[0] = a copy of the characters of str from character 0 to the * last character before the first instance of sep * parts[1] = a copy of the characters of str from the first character * after the first instance of sep to the last character * before the second instance of sep (or the end of the string, * if there is no second instance). * parts[i] = a copy of the characters of str from the first character * after the ith instance of sep (or the '\0' at the end of the * string if there are fewer than ith instances) to the * last character before the i+1st instance of sep (or the '\0' * at the end of the string). * Practica: * split ("a,b,c", ",", strings, 3) * strings[0] = "a" * strings[1] = "b" * strings[2] = "c" * split ("a", ",", strings, 3) * strings[0] = "a" * strings[1] = "" * strings[2] = "" * split ("a,b,c,d", ",", strings, 3) * strings[0] = "a" * strings[1] = "b" * strings[2] = "c" * split ("a,b,c,d", " ", strings, 3) * strings[0] = "a,b,c,d" * strings[1] = "" * strings[2] = "" * split ("lions and tigers and bears", " and ", animals, 3) * animals[0] = "lions" * animals[1] = "tigers" * animals[2] = "bears" */ void split (char *str, char *sep, char *parts[], int n); #endif // __SPLIT_H__