首页 > 解决方案 > 如何在C中存储给定定界符的子字符串

问题描述

假设我有一系列采用这种形式的数据:

"SomethingIDontCareAbout : SomethingICareAbout"

当然,“:”之后的部分长度可能会有所不同。

这里的目标只是有效地存储“SomethingICareAbout”子字符串。我做了这个函数,但问题是我存储了两个子字符串,所以这似乎是在浪费内存。任何有助于降低时间/空间复杂性?

char** ExtractKey(char* S)           
{
    int n = strlen(S);
    int count = 0, i = 0, j = 0;
    for(i = 0; i < n; i++)
    {
        if(S[i] == ':')
            break;
        
        count++;
    }
    char** T = (char**)malloc(2 * sizeof(char*));

    T[0] = (char*)malloc((count + 1)  * sizeof(char));
    T[1] = (char*)malloc((n - count) * sizeof(char));

    for(i = 0; i < count; i++)                          // inefficient ? cus we won't need T[0] [j]
    {
        T[0][j] = S[i];
        j++;
    }
    T[0][j+1] = '\0';
    j = 0;
    for(i = count + 1; i < n; i++)
    {
        T[1][j] = S[i];
        j++;
    }
    T[1][j+1] = '\0';
    return T;
}

标签: cstringperformancememorydelimiter

解决方案


strtok()老实说,如果用于拆分这些字符串,这可以有效地完成。:我设计了以下代码,该代码使用此处的通用分隔符解析二维数组的每个字符串。

现在,让我们看一下代码(注意注释):

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

#define MAX_LEN 128

int main(void) {
    // The 2-D string
    char str[][MAX_LEN] = {"SomethingElse : SomethingToCareAbout",
                           "Something2 : SomethingToCare2",
                           "Unnecessary : Necessary"};
    int size = sizeof(str) / sizeof(str[0]);

    // Applying Variable-Length Array (valid in C)
    char store_cared_ones[size][MAX_LEN];

    for (int i = 0; i < size; i++) {
        // Declaring a temporary pointer variable to obtain the required
        // substring from each string
        char *sub_str = NULL;
        sub_str = strtok(str[i], ": ");
        sub_str = strtok(NULL, ": ");

        // Copying the 'sub_str' into each array element of 'store_cared_ones'
        strcpy(store_cared_ones[i], sub_str);
    }

    // Displaying each of 'store_cared_ones'
    for (int i = 0; i < size; i++)
        fprintf(stdout, "%s\n", store_cared_ones[i]);

    return 0;
}

最后,让我们看看这段代码做了什么:

rohanbari@genesis:~/stack$ ./a.out
SomethingToCareAbout
SomethingToCare2
Necessary

推荐阅读