Adam Ramadhan
Adam Ramadhan

Reputation: 22810

Add Space between two string in C

hello im trying to add a space between two strings and strcopy them into a variable

in php we could just add them with a +

front_name = "hello";
back_name = "world";
full_name = strcpy(m[index].p.something, front_name + " " + backname);
// should output hello world

what is the equivalent or the right way to do it in c?

Upvotes: 2

Views: 2905

Answers (2)

suspectus
suspectus

Reputation: 17258

sprintf would be ideal provided the buffer that the strings are copied to is large anough:

e.g.

   char buffer[512]; 
   sprintf(buffer, "%s %s", front_name, backname);

Upvotes: 5

Yu Hao
Yu Hao

Reputation: 122383

Assuming that result has enough space, use this:

sprintf(result, "%s %s", front_name, backname);

Upvotes: 4

Related Questions