首页 > 代码库 > strsep函数实现及测试

strsep函数实现及测试

#include<stdio.h>
char *strsep(char **stringp, const char *delim)
{
    char *s;
    const char *spanp;
    int c, sc;
    char *tok;
    if ((s = *stringp)== NULL)
        return (NULL);
    for (tok = s;;) {
        c = *s++;
        spanp = delim;
        do {
            if ((sc =*spanp++) == c) {
                if (c == 0)
                    s = NULL;
                else
                    s[-1] = 0;
                *stringp = s;
                return (tok);
            }
        } while (sc != 0);
    }
    /* NOTREACHED */
}
int main()
{
        char *s3;
	char s1[] = "h,e,l,l,o,word";
	char *s2 = ",";
	char *buf;
	buf = s1;
	while((s3 = strsep(&buf,s2)) != NULL)
	{
	    printf("%s\n",s3);
	}
	return 0;

}

运行结果:
h
e
l
l
o
word

 

strsep函数实现及测试