Search code examples
arrayscstrcat

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


So I have 2 different sized arrays and I want to pair elements from them into a new one.

Example:

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}(The array I want to obtain)

So far I've got this, and it prints as follows: 1a 1ab 1abc...

I've managed to print them in the way I want with printf("%s%s", &a[i], &b[j]) but I still can't wrap my head on how to save this pairs in a separate array

    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);
        }
    }

I'm trying to learn C and I hit a wall at this part. Thank you for understanding and sorry if my english is bad


Solution

  • Character arrays

    If arrays a and b only contain characters, you don't even need the function strcat. I have provided example code in which array f contains nine strings with two characters plus the null character '\0' each.

    #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] = '\0';
            }
        }
    
        /* print content of the array `f` */
        for (int k = 0; k < 9 * 4; ++k)
                printf("%s ", f[k]);
        printf("\n");
    
        return EXIT_SUCCESS;
    }
    

    After compiling, the first program outputs the following when executed:

    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
    

    String arrays

    If arrays a and b are supposed to contain strings with more than one character, you will need to use strcpy and strcat from <string.h>. Choose an appropriate maximum size for the strings in 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;
    }
    

    After compiling, the second program outputs the following when executed:

    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