有没有办法从两个大小也不同的数组中配对?

Is there a way to make pairs from 2 different arrays that also have different sizes?

所以我有 2 个不同大小的数组,我想将它们中的元素配对成一个新数组。

示例:

a = {1 2 3 4 5 6 7 8 9}

b = {a b c d}

c = {1a 1b 1c 1d 2a 2b 2c 2d...9a 9b 9c 9d}(我要获取的数组)

到目前为止我得到了这个,它打印如下:1a 1ab 1abc...

我已经设法用 printf("%s%s", &a[i], &b[j]) 以我想要的方式打印它们,但我仍然无法理解如何将这些对保存在单独的数组中

    const char *a[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
    const char *b[] = {'a', 'b', 'c', 'd'};
    char *f;

    for(int i = 0; i < 9; i++){
        for(int j = 0; j < 4; j ++){
            f = (strcat(&a[i], &b[j]));
            printf("%s ", f);
        }
    }

我正在尝试学习 C,但在这部分碰壁了。感谢您的理解,如果我的英语不好,请见谅

我不太确定你需要什么...

如果你想要这样的输出: 1a 1b 1c 1d 2 2b 2c 2d 3 3b 3c 3d 4 4b 4c 4d 5 5b 5c 5d 6 6b 6c 6d 7 7b 7c 7d 8 8b 8c 8d 9 9b 9c 9d

最简单的解决方案是将 f 视为两个字符的数组。 因为 strcat (char *strcat(char *dest, const char *src);) 在第一个参数字符串之后连接第二个参数字符串,你不能简单地使用第一个字符作为目标,但你需要第三个变量,在你的情况下 f.

打印后,您必须重新初始化 (使用 strcpy 因为它是一个字符串) 它以便为下一对 space 设置。

    const char *a[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
    const char *b[] = {'a', 'b', 'c', 'd'};
    char f[2] = "";

    int i, j;

    for(i = 0; i < 9; i++)
    {
        for(j = 0; j < 4; j++)
        {
            strcat(f, &a[i]);
            strcat(f, &b[j]);
            printf("%s ",f);
            strcpy(f,"");
        }
    }

希望是你需要的!

EDIT -> 要保存您需要的第 4 个数组的值,大小为 (Asize x Bsize )

这很好,但仍有一个错误。我会马上修复:

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

#define NODE_LEN 2 + 1

void solution() {
    
    const char a[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
    const char b[] = {'a', 'b', 'c', 'd'};
    char *result_array[8] = { 0 };
    char f[NODE_LEN] = { 0 };
    int i, index_j;

    for (i = 0, index_j = 0; i < 9; i++) {
        
        sprintf(f, "%c%c", a[i], b[index_j]);
        printf("%s ", f);
        
        char* new_value = calloc(NODE_LEN, sizeof(char));
        strcpy(new_value, f);
        result_array[i] = new_value;
        
        if (++index_j >= 4) index_j = 0;
    }
    
    for (i = 0; i < 9; i++) {
        printf("-%s\n", result_array[i]);
    }
    
    for (i = 0; i < 9; i++) {
        free(result_array[i]);
    }
}

int main(void) {
    solution();
    
    return 0;
}

字符数组

如果数组 ab 只包含字符,您甚至不需要函数 strcat。我提供了示例代码,其中数组 f 包含九个字符串,每个字符串有两个字符加上 null character '[=18=]'

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

int main (void)
{
    /* arrays `a` and `b` only contain characters */
    char a[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
    char b[] = {'a', 'b', 'c', 'd'};

    /* array `f` contains 9 * 4 strings with 3 characters each */
    char f[9 * 4][3];

    for (int i = 0; i < 9; ++i) {
        for (int j = 0; j < 4; ++j) {
            int k = i * 4 + j;
            f[k][0] = a[i];
            f[k][1] = b[j];
            /* null character for string termination */
            f[k][2] = '[=10=]';
        }
    }

    /* print content of the array `f` */
    for (int k = 0; k < 9 * 4; ++k)
            printf("%s ", f[k]);
    printf("\n");

    return EXIT_SUCCESS;
}

编译后,第一个程序执行时输出如下:

1a 1b 1c 1d 2a 2b 2c 2d 3a 3b 3c 3d 4a 4b 4c 4d 5a 5b 5c 5d 6a 6b 6c 6d 7a 7b 7c 7d 8a 8b 8c 8d 9a 9b 9c 9d

字符串数组

如果数组 ab 应该包含多于一个字符的字符串,您将需要使用 [=23= 中的 strcpystrcat ].为 f.

中的字符串选择合适的最大大小
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (void)
{
    /* arrays `a` and `b` contain strings with multiple character */
    char *a[] = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
    char *b[] = {"_a", "_b", "_c", "_d"};

    /* array `f` contains 9 * 4 strings with a maximum of 15 characters*/
    char f[9 * 4][15];

    for (int i = 0; i < 9; ++i) {
        for (int j = 0; j < 4; ++j) {
            int k = i * 4 + j;
            strcpy(f[k], a[i]);
            /* `strcat` automatically null-terminates */
            strcat(f[k], b[j]);
        }
    }

    /* print content of the array `f` */
    for (int k = 0; k < 9 * 4; ++k)
            printf("%s ", f[k]);
    printf("\n");

    return EXIT_SUCCESS;
}

编译后,第二个程序执行时输出如下:

one_a one_b one_c one_d two_a two_b two_c two_d three_a three_b three_c three_d four_a four_b four_c four_d five_a five_b five_c five_d six_a six_b six_c six_d seven_a seven_b seven_c seven_d eight_a eight_b eight_c eight_d nine_a nine_b nine_c nine_d

这通常被称为置换函数。我们将需要 space 来存储我们的结果,这将是数组的长度 A 乘以数组的长度 B.

我们的单个结果将是 char [3] 类型,一个 space 用于数字,一个用于字母,一个用于正确要求的 NUL 终止字节 ('[=14=]')在 C.

中创建一个 string
const char a[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
const char b[] = {'a', 'b', 'c', 'd'};

char zipped[9*4][3];

for (int i = 0, k = 0; i < 9; i++) 
    for (int j = 0; j < 4; j++, k++) {
        zipped[k][0] = a[i];
        zipped[k][1] = b[j];
        zipped[k][2] = '[=10=]';
    }

for (int i = 0; i < 9 * 4; i++)
    printf("%s ", zipped[i]);

// 1a 1b 1c 1d 2a 2b 2c 2d 3a 3b 3c 3d 4a 4b 4c 4d 5a 5b 5c 5d 6a 6b 6c 6d 7a 7b 7c 7d 8a 8b 8c 8d 9a 9b 9c 9d

要将所有内容存储在单独的数组中,您需要通过 malloc(或类似方法)分配内存或指定固定大小的数组。

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

int main(int argc, char** argv) {
    const char a[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
    const char b[] = {'a', 'b', 'c', 'd'};
    char **f;
    
    // Getting the size of each array
    const int a_size = sizeof(a) / sizeof(a[0]);
    const int b_size = sizeof(b) / sizeof(b[0]);

    // Allocating the memory needed for the array of strings
    f = (char**) malloc(a_size * b_size * sizeof(char*));

    for(int i = 0; i < a_size; i++) {
        for(int j = 0; j < b_size; j++) {

            // Allocating memory for each combination pair. 1a, 1b...
            f[i * a_size + j] = (char*) malloc(3 * sizeof(char)); // Size of 3 is needed to include the null terminator '[=10=]'
            sprintf(f[i * a_size + j], "%c%c", a[i], b[j]);
            printf("%s ", f[i * a_size + j]);
        }
    }

    // Make sure to free all the allocated memory.
    for (int i = 0; i < a_size * b_size; i++) {
        free(f[i]);
    }
    free(f);
    return 0;
}

gcc编译它会输出

1a 1b 1c 1d 2a 2b 2c 2d 3a 3b 3c 3d 4a 4b 4c 4d 5a 5b 5c 5d 6a 6b 6c 6d 7a 7b 7c 7d 8a 8b 8c 8d 9a 9b 9c 9d