全部博文(26)
分类: C/C++
2009-08-06 10:12:34
已知strcpy()的函数原型:char *strcpy(char *dest, const char *src)其中dest是目的字符串,src是源字符串。不调用C++/C的字符串库函数,请编写函数strcpy()。
char *strcpy(char *dest, const char *src)
{
if (dest == NULL || src == NULL)
return NULL;
if (dest == src)
return dest;
char *tmp = dest;
while(*src) {
*dest = *src;
dest++;
src++;
}
*dest = '\0';
dest = tmp;
return dest;
}
char * strncpy(char * dest,const char *src,size_t count)
{
char *tmp = dest;
while (count) {
if ((*tmp = *src) != 0) src++;
tmp++;
count--;
}
return dest;
}