strcat函数是字符串链接函数,将* src字符串连接到* dest字符串的有效字符的后面,包括’\0’
//模拟实现strcat函数
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
char *my_strcat(char *dest, const char *src)
{
assert(dest);
assert(src);
char *tmp=dest;
while (*dest != '\0')
{
dest++;
}
while (*dest++ = *src++)
{
;
}
return tmp;
}
int main()
{
char a[20] = "abcdef";
char b[] = "ab";
strcat(a, b);
printf("%s\n",a);
system("pause");
return 0;
}
strncat函数是字符串链接函数,将* src字符串中n个字符连接到* dest字符串的有效字符的后面,最后还要加上’\0’
//模拟实现strncat函数
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
char *my_strncat(char *dest, const char *src, int count)
{
assert(dest);
assert(src);
char *tmp=dest;
while (*dest != '\0')
{
dest++;
}
while ((count--) && ( *src))
{
*dest++=*src++;
}
*dest = '\0';
return tmp;
}
int main()
{
char a[20] = "abcdef";
char b[] = "ab";
my_strncat(a, b, 5);
printf("%s\n",a);
system("pause");
return 0;
}