strncpy函数

/*
原型:char * strncpy(char *dest, char *src,size_tnum);
功能:(c/c++)复制src中的内容(字符,数字、汉字....)到dest,复制多少由num的值决定,返回指向dest的指针。
如果遇到null字符('\0'),且还没有到num个字符时,就用(num - n)(n是遇到null字符前已经有的非null字符个数)个null字符附加到destination。
注意:并不是添加到destination的最后,而是紧跟着由source中复制而来的字符后面。
*/

#include <stdio.h>

char* strncpy(char *dest, const char *src, size_t count)
{
	char *address = dest;

	while (count && (*dest = *src) != '\0')
	{
		src++;
		dest++;
		count--;
	}
	while(count--)
	{
		*dest++ = '\0';
	}

	return address;
}

int main ()
{
  char str1[]= "To be or not to be";
  char str2[40];
  char str3[40];

  /* copy to sized buffer (overflow safe): */
  strncpy( str2, str1, sizeof(str2) );

  /* partial copy (only 5 chars): */
  strncpy ( str3, str2, 5 );
  str3[5] = '\0';   /* null character manually added */

  puts (str1);
  puts (str2);
  puts (str3);

  return 0;
}


 

//百度百科上的实现

char *strncpy(char *dest, const char *src, int n)
{
    char c;
    char *s = dest;
    if (n >= 4)
    {
        size_t n4 = n >> 2;
        while(1)
        {
            c = *src++;
            *dest++ = c;
            if (c == '\0')
                break;
            c = *src++;
            *dest++ = c;
            if (c == '\0')
                break;
            c = *src++;
            *dest++ = c;
            if (c == '\0')
                break;
            c = *src++;
            *dest++ = c;
            if (c == '\0')
                break;
            if (--n4 == 0)
                goto last_chars;
        }
        n -= dest - s;
        goto zero_fill;
    }
last_chars:
    n &= 3;
    if (n == 0)
        return s;
    for (;;)
    {
        c = *src++;
        --n;
        *dest++ = c;
        if (c == '\0')
            break;
        if (n == 0)
            return s;
    }
zero_fill:
    while (n-- > 0)
        dest[n] = '\0';
    return s;
}


 

//linux源码

#ifndef __HAVE_ARCH_STRNCPY   
/** 
 * strncpy - Copy a length-limited, %NUL-terminated string 
 * @dest: Where to copy the string to 
 * @src: Where to copy the string from 
 * @count: The maximum number of bytes to copy 
 * 
 * The result is not %NUL-terminated if the source exceeds 
 * @count bytes. 
 * 
 * In the case where the length of @src is less than  that  of 
 * count, the remainder of @dest will be padded with %NUL. 
 * 
 */  
char *strncpy(char *dest, const char *src, size_t count)  
{  
    char *tmp = dest;  
  
    while (count) {  
        if ((*tmp = *src) != 0)  
            src++;  
        tmp++;  
        count--;  
    }  
    return dest;  
}  
EXPORT_SYMBOL(strncpy);  
#endif   


 

 

/***
*strncpy.c - copy at most n characters of string
*
*       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
*
*Purpose:
*       defines strncpy() - copy at most n characters of string
*
*******************************************************************************/

#include <cruntime.h>
#include <string.h>

/***
*char *strncpy(dest, source, count) - copy at most n characters
*
*Purpose:
*       Copies count characters from the source string to the
*       destination.  If count is less than the length of source,
*       NO NULL CHARACTER is put onto the end of the copied string.
*       If count is greater than the length of sources, dest is padded
*       with null characters to length count.
*
*
*Entry:
*       char *dest - pointer to destination
*       char *source - source string for copy
*       unsigned count - max number of characters to copy
*
*Exit:
*       returns dest
*
*Exceptions:
*
*******************************************************************************/

char * __cdecl strncpy (
        char * dest,
        const char * source,
        size_t count
        )
{
        char *start = dest;

        while (count && (*dest++ = *source++))    /* copy string */
                count--;

        if (count)                              /* pad out with zeroes */
                while (--count)
                        *dest++ = '\0';

        return (start);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值