/* Collection of code snippets by Arne Vajhøj */
/* (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) */
#include <stdio.h>
#include <string.h>

void gettok(char *s, int first, int last, char sep, char *res, int ressiz)
{
    int ixs, ixres, nsep;
    ixs = 0;
    ixres = 0;
    nsep = 0;
    while(ixs < strlen(s) && ixres < ressiz - 1)
    {
        if(first <= nsep+1 && nsep+1 <= last)
        {
            res[ixres] = s[ixs];
            ixres++;
        }
        if(s[ixs] == sep) 
        {
            nsep++;
            if(nsep+1 > last) ixres--;
        }
        ixs++;
    }
    res[ixres] = '\0';
    return;
}

int main()
{
    char buf[100];
    gettok("hej med dig", 2, 2, ' ', buf, sizeof(buf));
    printf("med = %s\n", buf);
    gettok("7.1.2.6.5.7", 4, 5, '.', buf, sizeof(buf));
    printf("6.5 = %s\n", buf);
    return 0;
}
