STRSPLIT(3pub)

✨ STRSPLIT πŸ’– (that's strsplit from the C Programmer's Manual, section 3pub, bestie)


πŸŽ€ NAME πŸŽ€

strsplit β€” split a string into words. it's giving "tokenizer but make it cute" πŸ’…


🌸 SYNOPSIS 🌸

#include <publib.h>
int strsplit(char *src, char **words, int maxw, const char *sep);

πŸ¦‹ DESCRIPTION πŸ¦‹

okay so strsplit takes your src string and splits it into words, where the words are separated by one or more of the characters hanging out in sep. and if sep is the empty string? no worries, it just uses whitespace characters, exactly like isspace(3) defines them. πŸ’«

the pointers to those words get stored into successive elements of the array that words points at. you get no more than maxw pointers stored, periodt. heads up though: the input string gets modified, because it swaps the separator character right after a word with '\0'. 😌

now here's the plot twist: if there are more than maxw words, only maxw-1 words come back, and the maxwth pointer in the array points to the rest of the string. and if maxw is 0? literally no modification happens. that's actually the whole point β€” it's how you count how many words there are, like, so you can dynamically allocate space for the word pointer table. iconic. πŸ‘‘

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]. πŸ’€ (yes bestie, read that one twice, it's a lot.)


🌈 RETURN VALUE 🌈

strsplit returns the total number of words in the input string. that's it, that's the tea. πŸ“


πŸ’œ EXAMPLE πŸ’œ

say your words are separated by white space and you just wanna count the words on a line β€” you could say this:

n = strsplit(line, NULL, 0, "");

and to print out the fields of a colon-separated list (think PATH, or a line from /etc/passwd or /etc/group), you might do this:

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 real life you would of course prefer not to restrict the number of fields, so you'd either allocate the pointer table dynamically (first counting the number of words using something like that first example), or β€” and this is the galaxy-brain move β€” realize that since it's the original string getting modified, you can do this instead:

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);

πŸ”₯ SEE ALSO πŸ”₯


πŸ‘‘ 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). 🫢


and that's the whole page bestie, mwah πŸ’‹ β€” this lil' baddie lives in Publib, filed under the C Programmer's Manual as STRSPLIT(3pub). go split some strings, you got this 🧚✨

πŸ“¦ where this page comes from