自己实现的my_strtok函数,不妥的地方请各位多多指教

来源:互联网 发布:杰森伯恩 知乎 编辑:程序博客网 时间:2024/04/24 17:26
#include <stdio.h>




static int is_substring( const char *dest, const char *src, int len)
{
    int i;
    for(i = 0;i < len; i++)
    {
        if(src[i] != dest[i])
        {
            return(0);
        }
    }
    return(1);
}


char *my_strtok(char *str, const char *delim)
{
    int i = 0;
    int len = strlen(delim);
    char *ret_p;
    char *temp;
    static char *save_p;
    temp = (str == NULL) ? save_p : str;
    ret_p = temp;


    if(1 == len)
    {
        /*****************************************/
     
        while(*temp != '\0')
        {
            if(*delim == *temp)
            {
                *temp = '\0';
                save_p = ++temp;
                return(ret_p);
            }
            temp++;
        }
       // return(NULL);
    }


    else
    {
        /******************************************/
        while(*temp != '\0')
        {
            if(is_substring(temp,delim,len) == 1)
            {
                *temp = '\0';
                save_p = temp + len;
                return(ret_p);
            }
            temp++;
        }


       // return(NULL);
    }


    return(NULL);
}
0 0
原创粉丝点击