I'm trying to find different substrings of information from a larger string where the information is separated by ":"
For example: username:id:password:info
How could I using strstr
function in C loop this string to extract the information into different substring?
So far I've tried something like:
char string[] = "username:id:password:info"
char *endptr;
if((endptr = strstr(string, ":")) != NULL) {
char *result = malloc(endptr - string + 1);
if(result != NULL) {
memcpy(result, string, (endptr - string));
result[endptr - string] = '\0';
username = strdup(result);
free(result);
}
}
I want to make this loopable to extract all substrings.