簡體   English   中英

C-使用for循環將2個數組連接在一起

[英]C - Concatenate 2 arrays in one using for loop

我想用C語言運行一個For Loop進行連接,然后將結果復制到新數組中。

    // 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()寫入二維char數組的每個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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM