The string handling functions in the C standard library is quite error prone so it's sometimes best to roll you own. Below is a solution which uses safe functions for reading a line of text and appending two strings.
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void ReadLine(char result[], int resultLen)
{
int ch, i;
assert(resultLen > 0);
i = 0;
ch = getchar();
while ((ch != '\n') && (ch != EOF)) {
if (i < resultLen - 1) {
result[i] = ch;
i++;
}
ch = getchar();
}
result[i] = '\0';
}
void Append(const char extra[], char dest[], int destLen)
{
int i, j;
i = 0;
j = strlen(dest);
while ((extra[i] != '\0') && (j + i < destLen - 1)) {
dest[j + i] = extra[i];
i++;
}
dest[j + i] = '\0';
}
int main(void)
{
char s1[10], s2[10];
int count, n;
ReadLine(s1, sizeof s1);
ReadLine(s2, sizeof s2);
count = scanf("%d", &n);
if ((count == 1) && (n >= 0)) {
Append(" ", s1, sizeof s1);
if (n < (int) sizeof s2) {
s2[n] = '\0';
}
Append(s2, s1, sizeof s1);
puts(s1);
} else {
fprintf(stderr, "Non-negative integer expected\n");
exit(EXIT_FAILURE);
}
return 0;
}