The first version naively assumes enough space in destination,
char *
my_strcat(char *destp, char *srcp)
{
if(!destp) return(destp); //check that dest is valid
if(!srcp) return(destp); //check that src is valid
char *dp=destp; //save original destp
while(*dp) { dp++; } //find end of destp
while(*srcp)
{
*dp++ = *srcp++; //copy src to dest, then increment
}
return(destp);
}
The second version allows you to specify maximum size for destination,
char *
my_strncat(char *destp, char *srcp, long max)
{
if(!destp) return(destp); //check that dest is valid
if(!srcp) return(destp); //check that src is valid
char *dp=destp; //save original destp
long x=0;
while(*dp && (x<max)) { dp++; ++x; } //find end of destp
while(*srcp && (x<max)) //copy from src while more space in dst
{
*dp++ = *srcp++; ++x; //copy src to dest, then increment
}
*dp = '\0'; //ensure destination null terminated
return(destp);
}
ahappens to already have 100 chars worth of text in it before you start the copy operation, or doesn't have a null terminator.strcat()