.\" part of publib .\" "@(#)publib-strutil:$Id: strsplit.3,v 1.2 1994/02/19 20:58:36 liw Exp $" .\" .TH STRSPLIT 3 "C Programmer's Manual" Publib "C Programmer's Manual" .SH NAME strsplit \- split string into words .SH SYNOPSIS .nf #include int \fBstrsplit\fR(char *\fIsrc\fR, char **\fIwords\fR, int \fImaxw\fR, const char *\fIsep\fR); .SH DESCRIPTION \fIstrsplit\fR splits the \fIsrc\fR string into words separated by one or more of the characters in \fIsep\fR (or by whitespace characters, as specified by \fIisspace\fR(3), if \fIsep\fR is the empty string). Pointers to the words are stored in successive elements in the array pointed to by \fIwords\fR. No more than \fImaxw\fR pointers are stored. The input string is modifed by replacing the separator character following a word with '\\0'. However, if there are more than \fImaxw\fR words, only \fImaxw\fR-1 words will be returned, and the \fImaxw\fRth pointer in the array will point to the rest of the string. If \fImaxw\fR is 0, no modification is done. This can be used for counting how many words there are, e.g., so that space for the word pointer table can be allocated dynamically. .PP strsplit splits the src string into words separated by one or more of the characters in sep (or by whitespace characters, as defined by isspace(3), if sep is the empty string). The src string is modified by replacing the separator character after each word with '\\0'. A pointer to each word is stored into successive elements of the array words. If there are more than maxw words, a '\\0' is stored after the first maxw-1 words only, and the words[maxw-1] will contain a pointer to the rest of the string after the word in words[maxw-2]. .SH "RETURN VALUE" \fIstrsplit\fR returns the total number of words in the input string. .SH EXAMPLE Assuming that words are separated by white space, to count the number of words on a line, one might say the following. .sp 1 .nf .in +5 n = strsplit(line, NULL, 0, ""); .in -5 .PP To print out the fields of a colon-separated list (such as PATH, or a line from /etc/passwd or /etc/group), one might do the following. .sp 1 .nf .in +5 char *fields[15]; int i, n; n = strsplit(list, fields, 15, ":"); if (n > 15) n = 15; for (i = 0; i < n; ++i) printf("field %d: %s\\n", i, fields[i]); .in -5 .PP In real life, one would of course prefer to not restrict the number of fields, so one might either allocated the pointer table dynamically (first counting the number of words using something like the first example), or realize that since it is the original string that is being modified, one can do the following: .sp 1 .nf .in +5 char *fields[15]; int i, n; do { n = strsplit(list, fields, 15, ":"); if (n > 15) n = 15; for (i = 0; i < n; ++i) printf("field %d: %s\\n", i, fields[i]); list = field[n-1] + strlen(field[n-1]); } while (n == 15); .in -5 .SH "SEE ALSO" publib(3), strtok(3) .SH AUTHOR The idea for this function came from C-News source code by Henry Spencer and Geoff Collyer. Their function is very similar, but this implementation is by Lars Wirzenius (lars.wirzenius@helsinki.fi)