C语言程序题!高分求答案!

不用系统函数strcpy()实现将字符串a复制到字符串b的功能!
以上是题目~~~~谢谢~~~
找到答案了...用不着那么麻烦吧...谢谢了~~~

第1个回答  2008-01-03
程序仅供参考:
如果字符串a比字符串b长的话 那么无法复制

如果字符串a比字符串b短的话 设字符串a的长度为n

char b[n];
for(i=0;i<n;i++)b[i]=a[i]
这样就可以了

如果字符串b的长度未定的话 就用strlen求出字符串的长度m 然后将这个长度赋给b就可以了

char b[m];
for(i=0;i<n;i++)b[i]=a[i]
第2个回答  2008-01-04
/***
*strcat.c - contains strcat() and strcpy()
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* Strcpy() copies one string onto another.
*
* Strcat() concatenates (appends) a copy of the source string to the
* end of the destination string, returning the destination string.
*
*******************************************************************************/

/***
*char *strcpy(dst, src) - copy one string over another
*
*Purpose:
* Copies the string src into the spot specified by
* dest; assumes enough room.
*
*Entry:
* char * dst - string over which "src" is to be copied
* const char * src - string to be copied over "dst"
*
*Exit:
* The address of "dst"
*
*Exceptions:
*******************************************************************************/

char * __cdecl strcpy(char * dst, const char * src)
{
char * cp = dst;

while( *cp++ = *src++ )
; /* Copy src over dst */

return( dst );
}

/* $OpenBSD: strcpy.c,v 1.9 2004/11/28 07:23:41 mickey Exp $ */

char *
strcpy(char *to, const char *from)
{
char *save = to;

for (; (*to = *from) != '\0'; ++from, ++to);
return(save);
}

取自
ftp://ftp.openbsd.org/pub/OpenBSD/3.9/sys.tar.gz 的 ./sys/lib/libkern/strcpy.c - BSD - C本回答被网友采纳
第3个回答  2008-01-03
void strcpy(char *a,n,char *b,m) \\函数名
{
char *p,*head; \\头指针
p=head=new [n+m]; \\开辟一个长度为a+b的数组
for(int i=0;i<n;i++,p++) \\把数组a赋给新开辟的空间
{
*p=*a;
}
for(i=0;i<m;i++,p++) \\\\把数组b赋给新开辟的空间
{
*p=*b;
}
a=head; \\把新开辟的数组首地址赋给原数组变量名a
}
delete[] a;
delete[] b;

就算原数组的空间不够,也可以用这个方法的.无须考虑要合并数组的长度
第4个回答  2008-01-03
补充一下:回答2虽然可以不用管目标的内存长度,但是会造成内存泄露。

对上面的回答1做了一下修改,没有调试:
void fun(char dest,char src,int m)//m为a字符串的长度
{
int i = 0;
int lenth = 0;

lenth = strlen(src); //没说strlen不能用吧

if(m>lenth)
{
for(i=0;i<lenth;i++) dest[i] = src[i]; //或者写成
}
else
{
for(i=0;i<m-1;i++) dest[i] = src[i];
}
}
第5个回答  2008-01-03
程序仅供参考:
void fun(char a[],char b[],int m,int n)//m为a字符串的长度
{int i; //n为b字符串的长度
if(m>n){
printf("The length of a string shorter than b string!");
return;
}
for(i=0;i<n;i++)
b[i]=a[i];
}
相似回答