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

 

posted @ 2017-08-14 21:40  Diligent_Murcielago  阅读(747)  评论(0编辑  收藏  举报