字符串拷贝并替换\0

来源:互联网 发布:索福瑞实时数据 编辑:程序博客网 时间:2024/05/29 02:07

#include <stdio.h>
#include <stdlib.h>

//字符串拷贝.把from所指字符串添加到to结尾处(覆盖dest结尾处的'\0')并添加'\0'.
char * strlcat(char * to, const char * from,unsigned int size_t)
{
    char * dest;
 int i=0,lenth=0;

 if(NULL==to || NULL==from || size_t<=1)return NULL;

 dest = to;

 size_t=size_t-1;
 while(*to != '\0')
 {
        to++;
     i++;
 }
    while (((*to=*from)!='\0') && i<size_t)
    {
  from++;
  to++;
  i++;
 }

 *to='\0';

 return dest;
}

int main (void)
{
 int ret=0;
 char cbuf[100]={"abcdefghijklmnopqrstuvwxyz!"};
 char dbuf[20]={"1234567890!"};

 strlcat(cbuf,dbuf,sizeof(cbuf));

 printf ("cbuf= %s\n",cbuf);
 printf ("dbuf= %s\n",dbuf);

 return 0;
}

 

原创粉丝点击