在C语言中将逗号分隔的字符串转换为数组

在C语言中将逗号分隔的字符串转换为数组,c,C,我有字符串指针st1,我想把它转换成数组字符串 char*st1=“最后一个,中间第一个,ID,信用” 我如何做到这一点?据我所知,没有标准的库函数可以做到这一点,但很容易创建自己的库函数。例如: #include <stdio.h> #include <string.h> #include <stdlib.h> void freeSplit( char ** arr ) { for( int i = 0 ; arr[i] != NULL ; i++

我有字符串指针st1,我想把它转换成数组字符串

char*st1=“最后一个,中间第一个,ID,信用”


我如何做到这一点?

据我所知,没有标准的库函数可以做到这一点,但很容易创建自己的库函数。例如:

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

void freeSplit( char ** arr )
{
    for( int i = 0 ; arr[i] != NULL ; i++ )
        free( arr[i] );
    free( arr );
}

char ** split( char * str, char * delim , char * trim )
{
    int c_start, c_end, c_count, c_pos;
    int t_start, t_end;
    char ** result;

    // count the commas:
    for( c_count = c_end = 0 ; str[c_end] ; c_end++ )
        if( strchr( delim, str[c_end] ) )
            c_count++;

    // allocate for result:
    result = malloc( sizeof(char*) * (c_count+2) );
    if( result == NULL )
        return NULL;

    // for each element:
    for( c_pos = c_start = c_end = 0 ; ; c_end++ )
    {
        if( strchr( delim, str[c_end] ) || str[c_end] == '\0' )
        {
            // 'trim' element
            t_start = c_start;
            t_end = c_end;
            if( trim != NULL ) // check if trim should be done
            {
                while( strchr( trim, str[t_start] ) )
                    t_start++;
                while( t_end > t_start && strchr( trim, str[t_end-1] ) )
                    t_end--;
            }

            // allocate space for element:
            result[c_pos] = malloc(sizeof(char)*( t_end - t_start + 1 ) );
            if( result[c_pos] == NULL )
            {
                freeSplit( result );
                return NULL;
            }

            // copy element:
            memcpy( result[c_pos], str + t_start, t_end-t_start );
            result[c_pos][t_end-t_start] = '\0';

            // for next loop:
            c_start = c_end + 1;
            c_pos++;
        }
        if( str[c_end] == '\0' ) // done?
            break;
    }
    result[c_pos] = NULL; // 'stop' element
    return result;
}

int main()
{
    char * st1 = "last,first middle, ID, credits";
    char ** string = split( st1, ",", " \t\n\r" );
    if( string )
    {
        for( int i = 0 ; string[i] != NULL ; i++ )
            printf( "(%s)", string[i] );
        printf( "\n" );
        freeSplit( string );
    }
    return 0;
}
#包括
#包括
#包括
无效自由碎片(字符**arr)
{
for(int i=0;arr[i]!=NULL;i++)
免费(arr[i]);
免费(arr);
}
字符**拆分(字符*str,字符*delim,字符*trim)
{
int c_开始、c_结束、c_计数、c_位置;
int t_开始,t_结束;
字符**结果;
//计算逗号:
for(c_count=c_end=0;str[c_end];c_end++)
if(strchr(delim,str[c_end]))
c_count++;
//分配结果:
结果=malloc(sizeof(char*)*(c_计数+2));
如果(结果==NULL)
返回NULL;
//对于每个元素:
对于(c_pos=c_start=c_end=0;c_end++)
{
if(strchr(delim,str[c|end])| str[c|end]=='\0')
{
//“修剪”元素
t_启动=c_启动;
t_端=c_端;
if(trim!=NULL)//检查是否应该进行修剪
{
while(strchr(trim,str[t_start]))
t_start++;
while(t_end>t_start&strchr(trim,str[t_end-1]))
结束;
}
//为元素分配空间:
结果[c_pos]=malloc(sizeof(char)*(t_end-t_start+1));
如果(结果[c_pos]==NULL)
{
自由分裂(结果);
返回NULL;
}
//复制元素:
memcpy(结果[c_pos],str+t_start,t_end-t_start);
结果[c_pos][t_end-t_start]='\0';
//对于下一个循环:
c_开始=c_结束+1;
c_pos++;
}
如果(str[c_end]='\0')//完成了吗?
打破
}
结果[c_pos]=NULL;//“停止”元素
返回结果;
}
int main()
{
char*st1=“最后,第一个中间,ID,信用”;
字符**字符串=拆分(st1,,,,“\t\n\r”);
如果(字符串)
{
for(int i=0;字符串[i]!=NULL;i++)
printf((%s)”,字符串[i];
printf(“\n”);
自由分裂(弦);
}
返回0;
}

您是否在请求C中的预初始化数组? 您的示例:char*st1=“最后一个,中间第一个,ID,credits”; 另外,我也不知道在你的例子中,你是否遗漏了第一个和中间的逗号。在这里的示例中,我假设它们是独立的元素:

// Initialize array of pointer

const char *st1[5] = { "last", "first", "middle", "ID", "credits" };
正如Weather Vane(上文)所指出的,标记字符串表示相对于某些分隔符拆分字符串。有许多方法可以标记字符串


有关标记化,请参阅本文:

探索
strtok()
strep()
等的使用。首先,明确您的需求。在您的示例中,第三个字符串是
“ID”
还是
“ID”
?是否可以有多个空格字符?如果可能,是否需要删除字段末尾的空格字符?其他空白字符(制表符、换行符等)呢?字段是否可能为空?(两个连续的逗号,可能中间有空格。)字段是否可能包含逗号,如果是,如何输入?(引号、转义字符等)编程最重要的是精确定义要解决的问题。
// Initialize array of pointer

const char *st1[5] = { "last", "first", "middle", "ID", "credits" };