C语言实现split函数

#include <stdio.h>
 #include <string.h>
 
void mySplit(char *src,const char *separator, char dest[][20], int *num)
{
    char *pPre = NULL;
    char *pNext = NULL;

     //记录分隔符数量
     int count = 0;
     //原字符串为空
     if (src == NULL || strlen(src) == 0)
        return;
    //未输入分隔符
     if (separator == NULL || strlen(separator) == 0)
        return;

     //获得第一个由分隔符分割的字符串
    pNext = strtok(src,separator);
    while(pNext != NULL) {
        //存入到目的字符串数组中
        pPre = pNext;
        pNext = strtok(NULL, separator);

        if (pPre && pNext)
        {
            rt_strncpy(dest[count++], pPre, (pNext-pPre-1));
        }
        else if(pPre)
        {
            strcpy(dest[count++], pPre);
        }
    }

    *num = count;
}
posted @ 2024-04-12 15:59  HL棣  阅读(134)  评论(0编辑  收藏  举报