jamie stevens'
code


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.



this page last modified @ 2006/11/30 13:41
website maintained by jamie stevens, school of mathematics and physics, university of tasmania
this page, its contents and style are the responsibility of the author, and do not necessarily reflect the opinions of the university of tasmania
all images on this site are copyright jamie stevens unless specified otherwise
best viewed at resolutions higher than 800x600 (this centre thing is 600 pixels wide) and with a recent browser
Firefox 2