[英]How to merge two 2D string arrays into one in C programming?
我想將兩個字符串數組合並為一個數組:
陣列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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.