cat strncat.c
/*
* Concatenates src onto the end of dest.
* Assumes enough space in dest.
* 目标指针空间必须有足够的存储空间。
* The strncat() function is similar, except that it will use
*at most n characters from src; and src does not need to be
*null terminated if it contains n or more characters.
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_LEN 255
char* my_strncat1(char* dst,const char* src,int n);
char* my_strncat2(char* dst,const char* src,int n);
int main()
{
char str1[MAX_LEN] = "zj,";
char* str2 = "ubuntuer!";
char* str3 = "debianer!";
printf("After strncat1:%s\n",my_strncat1(str1,str2,6));
printf("After strncat2:%s\n",my_strncat2(str1,str3,9));
exit(EXIT_SUCCESS);
}
char* my_strncat1(char* dst,const char* src,int n)
{
char* p = dst;
while(*p)
p++;
while((n--)&&(*src++!='\0'))
*p++=*src;
*p='\0';
return(dst);
}
/*这个貌似就有点没意思了哦,不过你还是不介意可以把自己写的
* strlen和strcpy copy过来呵呵^_^
* 自己去diy把*/
char* my_strncat2(char* dst,const char* src,int n)
{
char* p = dst;
p += strlen(dst);
strncpy(p,src,n);
*(p+strlen(dst))='\0';
return dst;
}
zj@zj:~/C_parm/string/own_str/strncat$ ./strncat
After strncat1:zj,buntue After strncat2:zj,buntuedebianer!
|