/* 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>

#define MAX_ELM 25
#define MAX_LEN 25

int split(char buf[MAX_ELM][MAX_LEN],char sep,char *s)
{
    int res,ix,i;
    res = 0;
    ix = 0;
    for(i=0;i<strlen(s);i++)
    {
        if(s[i]==sep)
        {
            if(ix>0)
            {
                buf[res][ix] = '\0';
                res++;
                ix = 0;
            }
        }
        else
        {
            buf[res][ix] = s[i];
            ix++;
        }
    }
    if(ix>0)
    {
        buf[res][ix] = '\0';
        res++;
    }
    return res;
}

int main()
{
    int n,i;
    char buf[MAX_ELM][MAX_LEN];
    n = split(buf,' ',"a bb ccc   dddd");
    for(i=0;i<n;i++)
    {
        printf("%s\n",buf[i]);
    }
    return 0;
}
