分类: C/C++
2010-07-30 15:32:48
cat strcat.c
/*
* Concatenates src onto the end of dest.
* Assumes enough space in dest.
* 目标指针空间必须有足够的存储空间。
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_LEN 255
char* my_strcat1(char* dst,const char* src);
char* my_strcat2(char* dst,const char* src);
int main()
{
char str1[MAX_LEN] = "zj,";
char* str2 = "ubuntuer!";
char* str3 = "centoser!";
printf("After strcat1:%s\n",my_strcat1(str1,str2));
printf("After strcat2:%s\n",my_strcat2(str1,str3));
exit(EXIT_SUCCESS);
}
char* my_strcat1(char* dst, const char* src )
{
char* p = dst;
while(*p)p++;
while((*p++ =*src++)!='\0')
;
return( dst );
}
/*这个貌似就有点没意思了哦,不过你还是不介意可以把自己写的
* strlen和strcpy copy过来呵呵^_^
* 自己去diy把*/
char* my_strcat2(char* dst,const char* src)
{
char* p = dst;
p += strlen(dst);
strcpy(p,src);
return dst;
}
zj@zj:~/C_parm/string/own_str/strcat$ ./strcat
After strcat1:zj,ubuntuer!
After strcat2:zj,ubuntuer!centoser!
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!