C - 使用 for 循环将 2 个数组连接成一个

C - Concatenate 2 arrays in one using for loop

我想运行一个C语言的For循环来连接,然后将结果复制到一个新数组中。

    // Simple array with numbers to be appended at the end of the array "type" below

char numbers[20][2]={"0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19"};

//after each of these words i want to append the number found above.

char type[10][30]={"rizi","makaronia","kafes","giaourti","feta","avga","sampuan","rouxa","aporipantiko","aposmitiko"};

//This is the array i wish to add the results. This way i will create 20 of each type

char random_type [20][30];

    int i,j;
    for(i = 0; i < 10; i++)
    {
        for (j = 0; i < 20; j++)
        {
            strcpy(random_type[i][j],type[j]);
        }

    }

可以进行一些简化,例如不需要连续数字数组。有一些错误,例如@iharob 和@Jasper 指出,OP 使用 strcpy() 写入二维字符数组的每个字符,这实际上是一个一维字符串数组。

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

#define WORDS   10
#define ANSWERS 20
#define NUMBERS 20

int main()
{
    // Words to have a number appended
    char type[WORDS][30]={"rizi","makaronia","kafes","giaourti","feta",
                       "avga","sampuan","rouxa","aporipantiko","aposmitiko"};
    //This is the array to create the results.
    char random_type [ANSWERS][35];
    int i, word, numb;
    srand ((unsigned)time(NULL));
    for(i=0; i<ANSWERS; i++) {
        numb = rand() % NUMBERS;
        word = rand() % WORDS;
        sprintf(random_type[i], "%s %d", type[word], numb);
    }
    for(i=0; i<ANSWERS; i++) {
        printf ("%s\n", random_type[i]);
    }
    return 0;
}

程序输出:

aporipantiko 19
makaronia 14
makaronia 9
aposmitiko 10
sampuan 6
feta 10
feta 2
giaourti 3
rizi 10
feta 8
sampuan 7
rouxa 4
rizi 8
giaourti 0
giaourti 19
aposmitiko 13
rouxa 2
avga 13
giaourti 13
aporipantiko 8

虽然,也许 OP 意味着用每个数字排列每个单词,在这种情况下我提供这个。

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

#define WORDS   10
#define NUMBERS 20

int main()
{
    // Words to have a number appended
    char type[WORDS][30]={"rizi","makaronia","kafes","giaourti","feta",
                       "avga","sampuan","rouxa","aporipantiko","aposmitiko"};
    //This is the array to create the results.
    char random_type [WORDS][NUMBERS][35];
    int i, j;
    for(i=0; i<WORDS; i++)
        for(j=0; j<NUMBERS; j++)
            sprintf(random_type[i][j], "%s %d", type[i], j);
    for(i=0; i<WORDS; i++)
        for(j=0; j<NUMBERS; j++)
            printf ("%s\n", random_type[i][j]);
    return 0;
}

根据 OP 评论,第三个解决方案创建了一个包含 600 个字符串的数组。

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

#define WORDS   10
#define NUMBERS 20

int main()
{
    // Words to have a number appended
    char type[WORDS][30]={"rizi","makaronia","kafes","giaourti","feta",
                       "avga","sampuan","rouxa","aporipantiko","aposmitiko"};
    //This is the array to create the results.
    char random_type [WORDS*NUMBERS][35];
    int i, j, k=0;
    for(i=0; i<WORDS; i++)
        for(j=0; j<NUMBERS; j++)
            sprintf(random_type[k++], "%s %d", type[i], j);
    for(k=0; k<WORDS*NUMBERS; k++)
            printf ("%s\n", random_type[k]);
    return 0;
}