C - Separate a string by quotations then spaces

Viewed 52

I want to separate a char* variable into a char** array by first the quotations then by spaces.

For instance,

char* str = "\"I am in quotes\" and I am not, however \" I am \"";
char* strCopy = malloc(strlen(str)+1)
strcpy(strCopy, str);

char** args = get_args(strCopy);

int i = 0;
while(args[i]) {
    printf("%s\n", args[i]);
    i++;
}

Would produce the output:

I am in quotes
and 
I
am
not,
however
I am

Part of the trouble is I also want to account for if the quotations are not closed, e.g.

char* str  = "\"My quotes are closed \" but \"mine are not";
My quotes are closed
but
"mine
are
not

I have attempted to use strtok, but I can't seem to get it to work this way.

0 Answers
Related