String Concatenation in C | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

String Concatenation in C

I want concatenation of First Name and Last Name with space in between to print Full Name, what should I do? Using Lib function strcat, there is no space, check my program below: https://code.sololearn.com/cDOEINGcetrL/?ref=app

7th Dec 2022, 3:02 AM
Wakil Ah Hamidi
Wakil Ah Hamidi - avatar
2 Answers
+ 5
If you just want to print both first name and last name, you can just add multiple arguments to printf: printf("%s %s", firstname, lastname); About using strcat it overwrites the char array that it's concatenating to (the first argument), so make sure the array size is big enough to store both parts. You can see an example here. https://code.sololearn.com/cByLH03nJxgd/?ref=app
7th Dec 2022, 5:20 AM
Tibor Santa
Tibor Santa - avatar
+ 4
Wakil Ahmad Hamidi you will have to add the space. You could strcat the space. I think a more convenient way is to use sprintf. It works like printf, except the output goes into a char array that you provide. Beware when you use strcat that the first char array must be large enough to hold both strings. To guard against overwriting past the end of the first array use strncat, which limits how many characters will go into the string. Remember to leave room for the terminating null character.
7th Dec 2022, 5:27 AM
Brian
Brian - avatar