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

时间:2015-01-18 19:58:50

标签: c arrays loops 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]);
        }

    }

1 个答案:

答案 0 :(得分:1)

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

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