将字符串分割在C分隔符 [英] Split string with delimiters in C

查看:93
本文介绍了将字符串分割在C分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样写一个函数来拆分并返回一个数组,在C编程语言?

分隔符字符串

 的char *海峡=一月,二月,三月,四月,五月,六月,七月,八月,九月,十月,十一月,十二月;
str_split(STR,,);


解决方案

您可以使用 的strtok() 功能分割字符串(并指定分隔符使用)。需要注意的是的strtok()将修改传递给它的字符串。如果原始字符串需要在其他地方做它的一个副本,并将副本传递给的strtok()

编辑:

例子(注意它不处理连续分隔符,JAN ,,,二月,三月为例):

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&ASSERT.H GT;焦炭** str_split(字符* a_str,为const char a_delim)
{
    焦炭**结果为0;
    为size_t计数= 0;
    字符* TMP = a_str;
    字符* last_comma = 0;
    字符DELIM [2];
    DELIM [0] = a_delim;
    DELIM [1] = 0;    / *计算有多少元素将被提取。 * /
    而(* TMP)
    {
        如果(a_delim == * TMP)
        {
            算上++;
            last_comma = tmp目录;
        }
        TMP ++;
    }    / *尾部的标记添加空间。 * /
    数+ = last_comma< (a_str +的strlen(a_str) - 1);    / *添加空间​​终止空字符串以主叫
       知道在哪里返回的字符串列表结束。 * /
    算上++;    结果=的malloc(sizeof的(字符*)*计);    如果(结果)
    {
        为size_t IDX = 0;
        字符*令牌= strtok的(a_str,DELIM);        而(令牌)
        {
            断言(IDX<计数);
            *(结果+ IDX +)=的strdup(令牌);
            令牌= strtok的(0,DELIM);
        }
        断言(IDX ==计数 - 1);
        *(结果+ IDX)= 0;
    }    返回结果;
}诠释的main()
{
    CHAR个月[] =一月,二月,三月,四月,五月,六月,七月,八月,九月,十月,十一月,十二月;
    焦炭**令牌;    的printf(个月= [%S] \\ n \\ n个月);    令牌= str_split(月,,);    如果(标记)
    {
        INT I;
        对于(i = 0; *(令牌+ I);我++)
        {
            的printf(月= [%S] \\ n,*(令牌+ I));
            免费(*(令牌+ I));
        }
        的printf(\\ n);
        免费的(标记);
    }    返回0;
}

输出:

  $ ./main.exe
个月= [一月,二月,三月,四月,五月,六月,七月,八月,九月,十月,十一月,十二月]月= [JAN]
月= [FEB]
月= [MAR]
月= [APR]
月= [可]
月= [JUN]
月= [JUL]
月= [AUG]
月= [SEP]
月= [OCT]
月= [NOV]
月= [DEC]

How do I write a function to split and return an array for a string with delimiters in the C programming language?

char* str = "JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC";
str_split(str,',');

解决方案

You can use the strtok() function to split a string (and specify the delimiter to use). Note that strtok() will modify the string passed into it. If the original string is required elsewhere make a copy of it and pass the copy to strtok().

EDIT:

Example (note it does not handle consecutive delimiters, "JAN,,,FEB,MAR" for example):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

char** str_split(char* a_str, const char a_delim)
{
    char** result    = 0;
    size_t count     = 0;
    char* tmp        = a_str;
    char* last_comma = 0;
    char delim[2];
    delim[0] = a_delim;
    delim[1] = 0;

    /* Count how many elements will be extracted. */
    while (*tmp)
    {
        if (a_delim == *tmp)
        {
            count++;
            last_comma = tmp;
        }
        tmp++;
    }

    /* Add space for trailing token. */
    count += last_comma < (a_str + strlen(a_str) - 1);

    /* Add space for terminating null string so caller
       knows where the list of returned strings ends. */
    count++;

    result = malloc(sizeof(char*) * count);

    if (result)
    {
        size_t idx  = 0;
        char* token = strtok(a_str, delim);

        while (token)
        {
            assert(idx < count);
            *(result + idx++) = strdup(token);
            token = strtok(0, delim);
        }
        assert(idx == count - 1);
        *(result + idx) = 0;
    }

    return result;
}

int main()
{
    char months[] = "JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC";
    char** tokens;

    printf("months=[%s]\n\n", months);

    tokens = str_split(months, ',');

    if (tokens)
    {
        int i;
        for (i = 0; *(tokens + i); i++)
        {
            printf("month=[%s]\n", *(tokens + i));
            free(*(tokens + i));
        }
        printf("\n");
        free(tokens);
    }

    return 0;
}

Output:

$ ./main.exe
months=[JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC]

month=[JAN]
month=[FEB]
month=[MAR]
month=[APR]
month=[MAY]
month=[JUN]
month=[JUL]
month=[AUG]
month=[SEP]
month=[OCT]
month=[NOV]
month=[DEC]

这篇关于将字符串分割在C分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆