Just write the function using its description, it's not terribly complex:
#include <stddef.h>
#include <string.h>
#include <stdio.h>
char* mystrsep(char** stringp, const char* delim)
{
char* start = *stringp;
char* p;
p = (start != NULL) ? strpbrk(start, delim) : NULL;
if (p == NULL)
{
*stringp = NULL;
}
else
{
*p = '';
*stringp = p + 1;
}
return start;
}
// Test adapted from http://www.gnu.org/s/hello/manual/libc/Finding-Tokens-in-a-String.html.
int main(void)
{
char string[] = "words separated by spaces -- and, punctuation!";
const char delimiters[] = " .,;:!-";
char* running;
char* token;
#define PRINT_TOKEN()
printf("token: [%s]
", (token != NULL) ? token : "NULL")
running = string;
token = mystrsep(&running, delimiters); /* token => "words" */
PRINT_TOKEN();
token = mystrsep(&running, delimiters); /* token => "separated" */
PRINT_TOKEN();
token = mystrsep(&running, delimiters); /* token => "by" */
PRINT_TOKEN();
token = mystrsep(&running, delimiters); /* token => "spaces" */
PRINT_TOKEN();
token = mystrsep(&running, delimiters); /* token => "" */
PRINT_TOKEN();
token = mystrsep(&running, delimiters); /* token => "" */
PRINT_TOKEN();
token = mystrsep(&running, delimiters); /* token => "" */
PRINT_TOKEN();
token = mystrsep(&running, delimiters); /* token => "and" */
PRINT_TOKEN();
token = mystrsep(&running, delimiters); /* token => "" */
PRINT_TOKEN();
token = mystrsep(&running, delimiters); /* token => "punctuation" */
PRINT_TOKEN();
token = mystrsep(&running, delimiters); /* token => "" */
PRINT_TOKEN();
token = mystrsep(&running, delimiters); /* token => NULL */
PRINT_TOKEN();
return 0;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…