实现库函数strncpy

twfx1027 2015-01-03 03:03:35
引用
#include <stdio.h>
char *mystrncpy( char *s, char *t, int n )
{
if( *s == NULL || *t == NULL )
return NULL;
char *tmp = s;
for( int i = 0; i < n; i++)
{
if( *t != '\0' )
{
*tmp++ = *t++;
}
else
{
*tmp++ = '\0';
}

}
*tmp = '\0';
return tmp;
}
void main()
{
char *s = "hello world!";
char *t = "boys";
printf( "%s\n", mystrncpy( s, t, 3 ) );
}
...全文
119 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
liangsc94 2015-01-04
  • 打赏
  • 举报
回复
你的问题挺不错的,代码写的还可以。面试时有时候就会考strcpy之类的。我照着一位大牛的写了一遍,跑通了。不知道对你有帮助没有,你看一下。
#include <stdio.h>
#define NDEBUG
#include <assert.h>
char*  my_strncpy(char *strDest,const char *strSrc,int n){
  assert((strDest!=NULL)&&(strSrc!=NULL));
    char *address=strDest;
    for(int i=0;i<n;i++)
	   *strDest++=*strSrc++;
  *strDest='\0';  
  return address;
}
int main(void){
  char array1[]="sdsad";
  char array2[]="hello world";
  char *strDest=array1;
  char *strSrc=array2;
  int n=5;
  my_strncpy(strDest,strSrc,8);
  printf("%s",strDest);
  return 0;
}
代码在GCC上面跑了一遍没问题的。 但有几个细节要注意:const char ×strSrc 源地址是一个常量。第二个考点:assert断言,程序的安全性。第三个考点:为了保证链式操作 可以返回一个指针。 你比我要聪明。。我这是前几天刚看到这个问题。 还有,要注意代码的精简性,没事多看一下源码,代码应追求短小精悍。
赵4老师 2015-01-04
  • 打赏
  • 举报
回复
C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\strncpy.c
/***
*strncpy.c - copy at most n characters of string
*
*       Copyright (c) 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);
}
Evankaka 2015-01-03
  • 打赏
  • 举报
回复
照着源码多写几次你就记得了,我觉得源码的写的很好啊 下面几句是主要的。。 char *temp=dst; while((*dst++=*src++)!='\0'); *dst='\0'; return dst;
ranky2009 2015-01-03
  • 打赏
  • 举报
回复
#include <stdio.h>



char *mystrncpy( char *des, const char *source, int n )
{
    if( des == NULL || source == NULL )
    return des;

    char *tmp = des;

    for( int i = 0; i < n; i++)
    {
        if( *source != '\0')
        {
            *tmp++ = *source++;
        }
    }
    *tmp = '\0';
    return des;
}

int main()
{
    char s[] = "hello world!";
    char *t = "boys";
    printf( "%s\n", mystrncpy( s, t, 3 ) );
    return 0;
}
c4641685 2015-01-03
  • 打赏
  • 举报
回复
return tmp? 你的tmp已经指向'\0'了吧? char* tmp=s之后,不要动tmp,这个tmp只是记录s的初始位置而已,将s赋值为你想要的结果即可。
twfx1027 2015-01-03
  • 打赏
  • 举报
回复
这个哪里错了 为什么运行不聊?谢谢

69,381

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧