简体   繁体   中英

How to merge two 2D string arrays into one in C programming?

I want to merge two string arrays into one array:

array 1:

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

and array 2:

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

and i want to put them in an array where the first name and last name can be together

First, include :

#include <string.h>

Now, let the string be stored in names[NUMBER_NAMES][LEN_NAMES]

char name[sNUMBER_NAMES][LEN_NAMES];

Finally, a for loop for each element in the array:

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM