tokstr
Routine for splitting strings by a delimiter
(replacement for the bad strtok)
written by Jamie Stevens 2006
Definition:
char *tokstr(char **curr_delim,char *delim,char *part)
Source:
tokstr.c
tokstr.h
This routine works in a similar fashion to strtok.
It takes three args:
- curr_delim - a pointer to a string which contains the string to parse.
- delim - a string containing the delimiter to break on
- part - a string which, upon return, will contain the next string token. This must be sent as a fully allocated char array.
Usage:
Begin by setting curr_delim to point to the beginning of the string you want to parse, and then call the tokstr function. Upon return, curr_delim will point to the letter directly after the first delimiter character, part will contain a the text between the start of the string and the delimiter (although not including the delimiter), and delim will not have changed. The value of curr_delim will also be the return value of the function. Subsequent calls to the function, using the curr_delim values returned from the function, will progress through the string. When no more text exists after curr_delim, the function will return NULL, and curr_delim will be set to NULL.
Example:
Consider the string "This is a short sentence."
To get each word in this string, the following code will work:
#include <stdio.h>
#include "tokstr.h"
#define BUFSIZE 256
void main(){
char sentence[BUFSIZE],*curr_delim,part[BUFSIZE];
int i;
strcpy(sentence,"This is a short sentence.");
for (i=1,curr_delim=sentence;
tokstr(&curr_delim," ",part)!=NULL;i++)
printf("word %d: %s\n",i,part);
}
This program would output:word 1: This word 2: is word 3: a word 4: short word 5: sentence.
