GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages


Manual Reference Pages  -  STRSPLIT (3)

NAME

strsplit - split string into words

CONTENTS

Synopsis
Description
Example
Author

SYNOPSIS

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

DESCRIPTION

strsplit splits the src string into words separated by one or more of the characters in sep (or by whitespace characters, as specified by isspace(3), if sep is the empty string). Pointers to the words are stored in successive elements in the array pointed to by words. No more than maxw pointers are stored. The input string is modifed by replacing the separator character following a word with ’\0’. However, if there are more than maxw words, only maxw-1 words will be returned, and the maxwth pointer in the array will point to the rest of the string. If maxw 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.

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].

RETURN VALUE

strsplit returns the total number of words in the input string.

EXAMPLE

Assuming that words are separated by white space, to count the number of words on a line, one might say the following.



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



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.

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, 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:

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

publib(3), strtok(3)

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)
Search for    or go to Top of page |  Section 3 |  Main Index


Publib STRSPLIT (3) C Programmer's Manual

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with manServer 1.07.