/*
  Demonstration of the strtok() function

  MAT 4970
  Bill Slough
*/

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

/* What characters are used to separate words? */
#define DELIMITERS " "

int main() {
    /* A simple string for illustration */
    char line[] = "   Four score and seven years ago our fathers brought forth";

    /* A pointer to be used by strtok() */
    char *ptr;

    printf("Before processing: \"%s\"\n", line);

    /* Find the first word in the line */
    ptr = strtok(line, DELIMITERS);
    
    while (ptr != NULL) {
	/* process the current word */
	printf("\"%s\"\n", ptr);

	/* get the next word in the line */
	ptr = strtok(NULL, DELIMITERS);  /* NB: line is NOT the first argument! */
    }

    /* Observe that strtok() modifies the string we have been scanning */
    printf("After processing: \"%s\"\n", line);
    
    return 0;
}
