1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
/* remember to do error checking, especially with the memory allocation and string length operations */
int main ()
{
const char *input_str = "abc.,def.,ghij.,klmn.,opq.,rst";
const char *str = input_str;
const char *delim = ".,";
const size_t str_length = strlen(str);
const size_t delim_len = strlen(delim);
char *pch = 0;
printf("str = %s\n", str);
while (pch = strstr(str, delim))
{
size_t len = pch - str;
pch += delim_len;
char *x = (char *) realloc(0, len + 1);
memset(x, 0, len + 1);
strncpy(x, str, len);
str = pch;
printf("x = %s\n", x);
free(x);
}
printf ("last token = %s\n", str);
return 0;
}
|