c - 如何在C编程中将两个二维字符串数组合并为一个?

标签 c arrays string 2d

我想将两个字符串数组合并为一个数组:

数组1:

firstnames[NUMBER_NAMES][LEN_NAME] = {"luca","tomas"} 

和数组 2:

secondname[NUMBER_NAMES][LEN_NAMES] = {"goirgi", "edison"}

我想将它们放入一个数组中,其中名字和姓氏可以在一起

最佳答案

首先,包括:

#include <string.h>

现在,让字符串存储在名称[NUMBER_NAMES][LEN_NAMES]中

char name[sNUMBER_NAMES][LEN_NAMES];

最后,对数组中的每个元素进行 for 循环:

for(i=0;i<NUMBER_NAMES;i++)
 { strcpy(names[i],firstnames[i]); //To initialize ith names element with first name
   strcat(names[i])," ");          //To concatanate a space between the 2 names   
   strcat(names[i]),lastnames[i]); //Concatanate the last name to the firstname+ space string
}

关于c - 如何在C编程中将两个二维字符串数组合并为一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17257276/

相关文章:

c - 基数转换c

c++ - 从 C++ 中的字符串中删除所有字符(a-z,A-Z)

c - 空间 : Need to identify only space not other characters

c# - 无法从 'string' 转换为 'system.collections.generic.list string'

javascript - JS通过函数增加索引号数组

javascript - 如何正确使用两个大写字母的 "undercore"

c - 如何仅计算列表中以大写字母开头的单词?

c - 在没有竞争条件的情况下,在 Windows 上将文件大小增加到至少 N

C 中的逗号和赋值运算符

javascript - 如何根据每个元素的长度对数组进行排序?