#include int parse_string(char *string, char ***word, char ***part) { char *args, *p = string; int i, count = 1; /* Trim leading white space, and duplicate string */ if(!p) return 0; while(isspace(*string)) string++; args = (char *)strdup(string); if(!args) return -1; /* Count words */ p = args; while(*p && *(p+1)) { if(isspace(*p) && !isspace(*(p+1))) count++; p++; } /* Allocate return pointers */ *part = (char **)calloc(count, sizeof(char*)); *word = (char **)calloc(count, sizeof(char*)); if(!part || !word) return -1; /* Set pointers */ i = 1; p = args; (*word)[0] = args; (*part)[0] = string; while(*p && *(p+1)) { if(isspace(*p)) { if(!isspace(*(p+1))) { (*word)[i] = p+1; (*part)[i] = string + ((p+1) - args); i++; } *p = '\0'; } p++; } return count; } int main(int argc, char **argv) { char *test = " \t This is\tonly a \t test \t "; char **word, **part; int count; count = parse_string(test, &word, &part); if(count > 0) { printf("Word %i = %s\n", count, part[count-1]); } else { printf("Parse failed: %i\n", count); } return 0; }