简体   繁体   English

根据字符拆分字符串数组

[英]Split an array of strings based on character

I have an array of strings similar to this: 我有一个类似于这样的字符串数组:

char* arrStrings[] = {"a", "be", "|", "cgg", "dss", "|", "mmd", "ddd", "\0"}

1) I would like to be able to be able to split this into a set of arrays, based on where the '|' 1)我希望能够根据'|'的位置将其拆分为一组数组 character is. 字符是。

So, after splitting, I would have this: 因此,分割后,我将得到:

a1 = {"a", "be", "\0"}
a2 = {"cgg", "dss", "\0"}
a3 = {"mmd", "ddd", "\0"}

2) After that, I need to create a linked list that has all 3 arrays, like this: 2)之后,我需要创建一个包含所有3个数组的链表,如下所示:

list = {pointer to a1, pointer to a2, pointer to a3}

I'm very confused about it, since there are multiple levels of pointers involved. 我对此非常困惑,因为其中涉及多个级别的指针。 How can I accomplish this? 我该怎么做?

If you can use NULL as delimiter in the list, you don't need to duplicate the strings, just use an array of pointers to string. 如果可以在列表中使用NULL作为分隔符,则无需复制字符串,只需使用指向字符串的指针数组即可。

*arrStrings[] = {"a", "be", "|", "cgg", "dss", "|", "mmd", "ddd", ""};

is transformed into 变成

*arr       [] = {"a", "be", NULL, "cgg", "dss", NULL, "mmd", "ddd", NULL};

Once you have filled the array you can use an array of pointers to pointer to string to create the list: 填充数组后,可以使用一个指向字符串的指针的数组来创建列表:

*arr       [] = {"a", "be", NULL, "cgg", "dss", NULL, "mmd", "ddd", NULL};
**ptr      [] = { ^                 ^                   ^               };

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

int main(void)
{
    /* The array */
    char *arrStrings[] = {"a", "be", "|", "cgg", "dss", "|", "mmd", "ddd", ""};
    /* Elements of the array */
    size_t sz = sizeof(arrStrings) / sizeof(arrStrings[0]);
    /* Loop */
    size_t i, n = 0;
    /* Create an array (VLA) of n pointers */
    char *arr[sz];

    /* Count the number of delimiters and fill array */
    for (i = 0; i < sz; i++) {
        /* If we found a delimiter assign NULL */
        if (strcmp(arrStrings[i], "|") == 0) {
            arr[i] = NULL;
            n++;
        } else
        /* If we found an empty string assign NULL */
        if (arrStrings[i][0] == '\0') {
            arr[i] = NULL;
        } else {
            arr[i] = arrStrings[i];
        }
    }
    /* Create an array (VLA) of n delimiters pointers to pointer */
    char **ptr[n + 1];
    ptr[0] = &arr[0];
    for (i = n = 0; i < sz - 1; i++) {
        /* For each NULL string push address of array + 1 into ptr */
        if (arr[i] == NULL) {
            ptr[++n] = &arr[i + 1];
        }
    }
    /* For each pointer to pointer loop and print until NULL */
    char **str;
    for (i = 0; i <= n; i++) {
        str = ptr[i];
        printf("%zu)\n", i);
        while (*str != NULL) {
            printf("\t%s\n", *str);
            str++;
        } 
    }
    return 0;
}

Output: 输出:

0)
    a
    be
1)
    cgg
    dss
2)
    mmd
    ddd

The first thing you have to know is that an array of strings is a pointer to a fixed number of further pointers to characters 您必须知道的第一件事是,字符串数组是指向固定数量的其他字符指针的指针

arrString --> |_|_|_| ... |_|
               | | |       |
               V V V       V
               a b |       \0
                 e

therefore you cannot simply update the "|" 因此,您不能简单地更新“ |” element with "\\0" and manage the string as it would be the "strtok" function for example. 元素带有“ \\ 0”并管理字符串,因为它例如是“ strtok”函数。 Rather, you have to allocate three new "char**" array to maintain the three "char*" sequences, and use them to create the list as you prefer. 相反,您必须分配三个新的“ char **”数组来维护三个“ char *”序列,并根据需要使用它们来创建列表。

In case you want to avoid the allocation of new memory space, then you have to implement customized facilities, in order to parse the "char**" array according to its formatting. 如果要避免分配新的内存空间,则必须实现自定义功能,以便根据其格式解析“ char **”数组。 In this way you can really have three pointers to three different positions in "arrString", which define the first sub-string that belongs to the interested array. 这样,您实际上可以拥有三个指向“ arrString”中三个不同位置的指针,这些指针定义了属于感兴趣数组的第一个子字符串。 The final element "|" 最后一个元素“ |” or "\\0" will indicate no further elements belong to it. 或“ \\ 0”表示没有其他元素属于它。 However, as I already said, this requires you implement ad-hoc functions, which avoid to alter the array. 但是,正如我已经说过的那样,这需要您实现即席功能,避免更改数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM