I was interviewed recently and asked to write mystrcat(*s1, *s2, *s3)
where s1
and s2
are source string and the concatenated results are given by s3
. I was told, don't worry about memory allocation of s3
and assume s1
and s2
are not null / invalid strings. So I wrote the following lame (crude) program. I was told there is something wrong with s3
or something can go wrong with s3
. Could you please tell what it is/ could that be?
void mystrcat(char *s1, char *s2, char *s3)
{
if (! (s1 || s2 || s3)) return; // one/more pointers are invalid
// copy string s1 into s3
while(*s1) {
*s3 = *s1;
s1++;
s3++;
}
// concatenate string s2 into s3
while(*s2) {
*s3 = *s2;
s2++;
s3++;
}
*s3 = '\0';
}
Could you please tell me what is wrong here? What would be more professional way of doing it?