一、strcat(dest, src)会将字符串src追加到字符串dest的尾部,dest最后的结束字符’\0’会被覆盖掉,并在连接后的字符串的尾部再添加一个’\0’。注意:dest要有足够的空间来容纳要追加的字符串。
char* my_strcat(char* dest, char* src)
{
int i = 0;
int j = 0;
while(dest[i] != '\0')
i++;
while(src[j] != '\0')
{
dest[i] = src[j];
i++;
j++;
}
dest[i] = '\0';
return dest;
}
二、strncat(dest, src, n)会将字符串src所指向的前n个字符追加到dest的结尾处,并在连接后的字符串的尾部再添加一个’\0’。
char* my_strncat(char* dest, char* src, int n)
{
int i = 0;
int j = 0;
while(dest[i] != '\0')
i++;
while(src[j] != '\0' && j < n)
{
dest[i] = src[j];
i++;
j++;
}
dest[i] = '\0';
return dest;
}