绝密飞行 mp4磁力:用函数表示出字符串复制的功能(不用strcpy)

来源:百度文库 编辑:神马品牌网 时间:2024/04/29 07:53:28

//我给出了两个版本的,因为我不知道楼主用的是什么语言
//请楼主仔细看一下copystr函数中的那个循环,很无敌的哦!
//我已经运行成功,可以直接拷贝过去运行.
//这个是c++版本的
#include <iostream>
using namespace std;
char * copystr(char *dest,char *source)
{
for(int i=0;dest[i]=source[i++];)
;
return source;
}
void main()
{
char a[128];
char b[128];
int i=0;
//初始化为0
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
//输入字符串
cin>>a;
//拷贝
copystr(b,a);
//打印输出
cout<<b;
}

/*
这个是c语言版本的
*/
#include <stdio.h>
char *copystr(char *dest,char *source)
{
int i;
for(i=0;dest[i]=source[i++];)
;
return source;
}
void main()
{
char a[128];
char b[128];
int i;
i=0;

/*初始化字符串为0*/
for(i=0;i<128;++i)
{
a[i]=0;
b[i]=0;
}

scanf("%s",a);
copystr(b,a);
printf("%s",b);
}

新建一个长度和源串相同的串(即CHAR[]),然后逐位赋值就行了。