Gnocl Cookbook‎ > ‎

    Parsing Strings into Words

    Gnocl options often take lists as arguments. For the most part these are of fixed length such as x y coordinate pairs but sometimes this might be a list of arbitrary length. The process of breaking such strings down into their components is achieved using the C library function strtok. The following example illustrates how it works.
    /* strtok example */
    #include <stdio.h>
    #include <string.h>

    int main ()
    {
    char str[] ="- This, a sample string.";
    char * pch;
    printf ("Splitting string \"%s\" into tokens:\n",str);
    pch = strtok (str," ,.-");
    while (pch != NULL)
    {
    printf ("%s\n",pch);
    pch = strtok (NULL, " ,.-");
    }
    return 0;
    }

    Which outputs:

    Splitting string "- This, a sample string." into tokens:
    This
    a
    sample
    string


    Sections