Question: Write a function in C that splits a string of characters depending on a separator. The function will written a custom struct array. Example usage
Write a function in C that splits a string of characters depending on a separator. The function will written a custom struct array.
Example usage should return an array of each substring.
possibly incorrect test usage:
int main
char teststr "abc def gh;
char testsep ;
stringarray res stringarraymallocsizeofstringarray;
res mysplitteststr testsep; should return abd def gh
printfs
resarray;
freeres;
return ;
mysplit.c
typedef struct sstringarray
int size;
int subs;
char array;
stringarray;
#endif
stringarray mysplitchar str char csep
stringarray arr stringarraymallocsizeofstringarray;
arrsize ;
arrsubs ;
arrarray NULL;
const char tmpstr;
tmpstr str;
while tmpstr
while tmpstr cseptmpstr;
iftmpstr
arrsize;
while tmpstr && tmpstr csep;
int found ;
while found && str
while str csepstr;
if str
const char m str; first match
while str && str csep;
sizet sz str ml;
char mcur mallocsz;
found mcur NULL;
if found
char tmp reallocarrarray, arrsubs sizeofchar ;
found tmp NULL;
iffound
arrarray tmp;
strncpymcur, ml sz;
mcursz;
arrrarrayarrsubs mcur;
else
freemcur;
return arr;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
