int split(s,word,max)
char *s; /* null-terminated string */
char *word[]; /* pointers to words of string */
int max; /* max number of words to be found */
The split function splits string s
into tokens (words)
word[0]
,word[1]
,...,word[max-1]
interpreting spaces and commas as
delimiters. Since split writes an EOS character in place of every word
ending with a space or a comma in s
, the original contents of s
are
destroyed during the split call. After the call, the pointers
word[0]
,word[1]
,... indicate the starting positions of the words in s
.
Please note that the words will be destroyed if the contents of s
are
altered after the split call.
split returns the number of words found which is max
at most. Thus if
the number of words in s
is greater than max
, the excessive words will
not be found. There is no error return.
char x[]="PRINT 11,20";
char *word[3];
int i,k;
k=split(x,word,3);
for (i=0; i<k; ++i)
printf("\nword[%d]=%s",i,word[i]);
prints:
word[0]=PRINT
word[1]=11
word[2]=20
split is the common tool when analyzing edit lines. A typical combination is, for example:
edread(x,j);
k=split(x+1,word,10); /* x+1=jth edit line without a control character */