FAQ > Separate a string into tokens? (C)


Match word(s).

If you have any questions or comments,
please visit us on the Forums.

FAQ > How do I... (Level 3) > Separate a string into tokens? (C)

This item was added on: 2003/08/20

There are many ways to split a line of text up into tokens. One simple example is to split up the words in a sentence. The following code shows how to do this using the strtok() function. Just beware that this function alters the original string, so if you need to use it again, make a copy before calling strtok().


/* Code supplied by Prelude */

#include <stdio.h> 
#include <string.h> 

#define DELIM   " " 
#define MAXWORD 80 
#define MAXLEN  20 

int main(void)
{
  char  words[MAXWORD][MAXLEN];
  char  buff[BUFSIZ];
  int   ntokens = 0;
  int   i;

  printf("Enter a string: ");
  fflush(stdout);

  if (fgets(buff, sizeof buff, stdin) != NULL)
  {
    char  *sep = strtok(buff, DELIM);

    while (sep != NULL)
    {
      strcpy(words[ntokens++], sep);
      sep = strtok(NULL, DELIM);
    }
  }

  for (i = 0; i < ntokens; i++) 
  {
    puts(words[i]);
  }

  return(0);
}

/*
 * Output :
  Enter a string: this is a long line of text
  this
  is
  a
  long
  line
  of
  text
 *
 */

Script provided by SmartCGIs