c++ 将字符串str1复制到str2中(不能使用strcpy()函数),速求

如题所述

#include <stdio.h>
char* strpy(char* str,char* dest) //字符串复制
{
char *ch=dest;
while(*str!='\0')
*ch++=*str++;
*ch='\0';
return dest;
}
int main()
{
char ch[20]="123456789";
char str[20]={0};
strpy(ch,str);
printf("%s\n",str);
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2012-11-11
头文件#include<string>
函数中定义
string str1=******** ,str2;
str2=str1;追问

我看不懂,有完整的程序吗

追答

c++中有string类型。添加头文件
#include可用。
添加后就相当于int、float、类型一样直接传值。

第2个回答  2012-11-11
char str1[]="hello";
char str2[100];
char *src=str1;
char *dst=str2;

while(*src)
{
*dst++=*src++;

}追问

# include "stdio.h"
# include "string.h"
main()
{
char str1[]="hello";
char str2[100];
char *src=str1;
char *dst=str2;

while(*src)
{
*dst++=*src++;

} }
运行不了啊

追答

最后加一句
*dst =0;

相似回答