How do I write a function to split and return an array for a string with delimiters in the C programming language?
char* str = "JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC";
str_split(str,',');
How do I write a function to split and return an array for a string with delimiters in the C programming language?
char* str = "JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC";
str_split(str,',');
Yet another answer (this was moved here from here):
Try to use the strtok function:
see details on this topic here or here
The issue here is that you have to process the
words
immediately. If you want to store it in an array you have to allocate thecorrect size
for it witch is unknown.So for example:
Note: We use the same loop and function to calculate the counts (pass one) and for making the copies (pass two), in order to avoid allocation problems.
Note 2: You may use some other implementation of the strtok the reasons mention in separate posts.
You can use this like:
(I did not test it, so please let me know if it does not work!)