自定义c语言字符串拷贝函数strcpy

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义c语言字符串拷贝函数strcpy相关的知识,希望对你有一定的参考价值。

/*
原 串 : Windows Application
目标串 : Windows Application
请按任意键继续. . .
*/
#include <stdio.h>
#include <stdlib.h>
char *strcopy(char ds[], char ss[])
int i = 0;
while(ds[i] = ss[i]) ++i;
return ds;


int main()
char s[] = "Windows Application";
char d[20];
printf("原 串 : %s\n",s);
printf("目标串 : %s\n",strcopy(d,s));
system("pause");
return 0;
参考技术A #include <stdio.h>
char* cpystr(char *des, char *res)

for(int i= 0; res[i]!='\0'; i++)
des[i]= res[i];
des[i]= '\0';
return des;

int main (void)

char d[30], s[]= "12345";
cpystr(d, s);
puts(d);
return 0 ;
本回答被提问者和网友采纳
参考技术B char* mystrcpy(char* dest, const char* src)
char* tmp = dest;
while (*tmp++ = *src++)
;

return dest;

C语言--strcpy()函数

strcpy,即string copy(字符串复制)的缩写。
strcpy是一种C语言的标准库函数,strcpy把含有‘\0‘结束符的字符串复制到另一个地址空间,返回值的类型为char*。

C语言 strcpy() 函数用于对字符串进行复制(拷贝)。

头文件:string.h

语法/原型:

char* strcpy(char* strDestination, const char* strSource);

参数说明:

  • strDestination:目的字符串。
  • strSource:源字符串。


strcpy() 会把 strSource 指向的字符串复制到 strDestination。

必须保证 strDestination 足够大,能够容纳下 strSource,否则会导致溢出错误。

返回值:目的字符串,也即 strDestination。

【实例】使用C语言 strcpy() 函数将字符串 src 复制到 dest。

#include <stdio.h>
#include <string.h>
int main()
    char dest[10] =  0 ;
    char src[10] =  "liangchen" ;
    strcpy(dest, src);
    puts(dest);
    return 0;

//输出:liangchen

  

以上是关于自定义c语言字符串拷贝函数strcpy的主要内容,如果未能解决你的问题,请参考以下文章

C语言 字符串操作函数及内存拷贝函数归总

c语言中strcpy跟mencpy哪个效率更高?

C语言 strcpy 函数

memcpy和strcpy的区别

C语言 strcpy_s 函数

编写C语言的字符串拷贝函数