【问题标题】:C - Concatenate 2 arrays in one using for loopC - 使用 for 循环将 2 个数组合二为一
【发布时间】:2015-01-18 19:58:50
【问题描述】:

我想用 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]);
        }

    }

【问题讨论】:

  • "10" 不适合 char[2] 数组,因为终止的零字节。
  • 我不清楚random_type的元素的期望值是什么。
  • 无限循环->for (j = 0; i &lt; 20; j++),还有random_type[i][j]是什么?
  • @Jasper 我已经设置了字符数[20][2],我已经看到它很适合这样。不是吗?
  • 长度为 2 的字符串需要一个大小为 3 的 char 数组来包含它,以及它的终止符。

标签: c arrays loops for-loop


【解决方案1】:

可以进行一些简化,例如不需要连续数字的数组。有一些错误,如@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 cmets 之后,第三种解决方案创建了一个包含 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;
}

【讨论】:

  • 谢谢!我真正需要的是在一个数组中添加总共 200 个结果的信息,以便我可以将其传输到结构中。
  • @falsobuio 如果我的回答有用请“采纳”,谢谢。
  • @falsobuio 我的答案的第二部分在一个数组中总共创建了 200 个结果,由于大小,我没有打印它们。
  • @falsobuio 我添加了第三部分,它创建了一个包含 200 个字符串的字符串数组。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多