c语言之split实现

#include 
#include 

int strsplit (const char *str, char *parts[], const char *delimiter) 
{
  char *pch;
  int i = 0;
  char *copy = NULL, *tmp = NULL;

  copy = strdup(str);
  if (! copy)
    goto bad;

  pch = strtok(copy, delimiter);

  tmp = strdup(pch);
  if (! tmp)
    goto bad;

  parts[i++] = tmp;

  while (pch) {
    pch = strtok(NULL, delimiter);
    if (NULL == pch) break;

    tmp = strdup(pch);
    if (! tmp)
      goto bad;

    parts[i++] = tmp;
  }

  free(copy);
  return i;

 bad:
  free(copy);
  for (int j = 0; j < i; j++)
    free(parts[j]);
  return -1;
}

void test001()
{
  char str[] = "hello\nworld\n1024";
    char *parts[8];
    size_t size = strsplit(str, parts, "\n");
    int i = 0;
    for (; i < size; ++i) {
        printf("%s\n", parts[i]);
    }
}
void test002()
{
    char str[] = "Flight attendants of Chengdu Airlines take a photo in front of the regional jet ARJ21-700 at Chengdu Shuangliu International Airport in Southwest China's Sichuan province on June 28, 2016. [Photo/Xinhua]";
    char *parts[1024];
    size_t size = strsplit(str, parts, " ");
    int i = 0;
    for (; i < size; ++i) {
        printf("%s\n", parts[i]);
    }
}
void test003()
{
  char str[] = "hello\u4e2d\u65871024world\u4e2d\u65871024nihao\u4e2d\u6587";
    char *parts[8];
    size_t size = strsplit(str, parts, "1024");
    int i = 0;
    for (; i < size; ++i) {
        printf("%s\n", parts[i]);
    }
}
void test004()
{
  char str[] = "hello中文1024world中文1024nihao中文";
    char *parts[8];
    size_t size = strsplit(str, parts, "中文");
    int i = 0;
    for (; i < size; ++i) {
        printf("%s\n", parts[i]);
    }
}
//https://github.com/jwerle/strsplit.c
int main (void) {
    //test001();
    test003();
    test004();
    return 0;
}
//使用strtok
#include 
#include 
int strsplinum(char *str, const char*del) //判断总共有多少个分隔符,目的是在main函数中构造相应的arr指针数组
{
    char *first = NULL;
    char *second = NULL;
    int num = 0;

    first = strstr(str,del);
    while(first != NULL)
    {
        second = first+1;
        num++;
        first = strstr(second,del);
    }
    return num;
}

void split( char **arr, char *str, const char *del)//字符分割函数的简单定义和实现
{
    char *s =NULL;

    s=strtok(str,del);
    while(s != NULL)
    {
        *arr++ = s;
        s = strtok(NULL,del);
    }
}
void test()
{
    printf("test start\n");
    //char *str="aaaa|bbbb|cccc|1024|你好|java";
    char* str="webview/*;lb*.xml;filter.xml";
    char *s =NULL;
    const char *del = ";"; //分隔符

    s=strtok(str,del);//s不用分配内存,只是指针遍历
    while(s != NULL)
    {
        printf("%s\n",s);
        s = strtok(NULL,del);
    }
    printf("test end\n");   
}

int main()
{
    char str[] = "aaaa|bbbb|cccc";
    const char *del = "|"; //分隔符
    int numTest = 1;
    int i=0;
    char *arr[4]; //利用指针数组对应分割好的字符串数组(即二维数组中的行数组)

    numTest = strsplinum(str,del);
    printf("the numbers of arry is : %d \n",numTest+1);
    split(arr,str,del);

    while(i<=numTest)
    {
        printf("%s\n",*(arr+i++)); //打印分割好的字符串
    }
    test();
    return 0;
}

你可能感兴趣的:(linux_c,c/c++)