
用c++编写strcpy
展开全部
/*
* An implementation of strcpy.
*/
char *strcpy1(char *target, const char *source)
{
int i;
for(i = 0; source[i] != '\0'; ++i)
target[i] = source[i];
target[i] = source[i];
return target;
}
#include <stdio.h>
int main()
{
char s1[] = "Hi there, fred!";
char s2[40];
strcpy(s2, s1);
printf("%s\n", s2);
}
* An implementation of strcpy.
*/
char *strcpy1(char *target, const char *source)
{
int i;
for(i = 0; source[i] != '\0'; ++i)
target[i] = source[i];
target[i] = source[i];
return target;
}
#include <stdio.h>
int main()
{
char s1[] = "Hi there, fred!";
char s2[40];
strcpy(s2, s1);
printf("%s\n", s2);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
char* strcpy(char* des,const char* source)
{
char* r=des;
assert((des != NULL) && (source != NULL));
while((*des++ = *source++)!='\0');
return r;
}
{
char* r=des;
assert((des != NULL) && (source != NULL));
while((*des++ = *source++)!='\0');
return r;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
从安全角度看,这个还是不安全的,考虑到串的长度;建议用s_strcpy
char *strcpy1(char *target, const char *source)
{
if( !target || !source )
return NULL;
char *head = target;
while( *target-- = *source-- );
return head;
}
#include <stdio.h>
int main()
{
char s1[] = "Hi there, fred!";
char s2[40];
strcpy1(s2, s1);
printf("%s\n", s2);
}
char *strcpy1(char *target, const char *source)
{
if( !target || !source )
return NULL;
char *head = target;
while( *target-- = *source-- );
return head;
}
#include <stdio.h>
int main()
{
char s1[] = "Hi there, fred!";
char s2[40];
strcpy1(s2, s1);
printf("%s\n", s2);
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询