手机
当前位置:查字典教程网 >编程开发 >C语言 >将字符串str1复制为字符串str2的三种解决方法
将字符串str1复制为字符串str2的三种解决方法
摘要:1.自己编写函数,将两个字符串进行复制复制代码代码如下:#includeusingnamespacestd;intmain(){charst...

1.自己编写函数,将两个字符串进行复制

复制代码 代码如下:

#include<iostream>

using namespace std;

int main(){

char str1[]="I love China!",str2[20];

void Strcpy(char *p1,char *p2);

Strcpy(str2,str1);

cout<<"str1: "<<str1<<endl;

cout<<"str2: "<<str2<<endl;

return 0;

}

void Strcpy(char *p2,char *p1){

int i=0;

for(;*p1!='';p1++,p2++){

*p2=*p1;

}

*p2='';

}

2.使用函数库重的strcpy函数

复制代码 代码如下:

#include<iostream>

using namespace std;

int main(){

char str1[]="I love China!",str2[20];

strcpy(str2,str1);

cout<<"str1: "<<str1<<endl;

cout<<"str2: "<<str2<<endl;

return 0;

}

3.定义两个字符串变量,然后直接进行赋值

复制代码 代码如下:

#include<iostream>

#include<string>

using namespace std;

int main(){

string str1="I love China!",str2;

str2=str1;

cout<<"str1: "<<str1<<endl;

cout<<"str2: "<<str2<<endl;

return 0;

}

【将字符串str1复制为字符串str2的三种解决方法】相关文章:

字符串拷贝函数memcpy和strncpy以及snprintf 的性能比较

c语言中用字符串数组显示菜单的解决方法

求子数组最大和的解决方法详解

字符串中找出连续最长的数字字符串的实例代码

基于VC中使用ForceInclude来强制包含stdafx.h的解决方法

c++ 一个二进制串转化为整数的解决方法

解析如何在C语言中调用shell命令的实现方法

C++多字节字符与宽字节字符相互转换

Eclipse对printf()不能输出到控制台的快速解决方法

深入C语言把文件读入字符串以及将字符串写入文件的解决方法

精品推荐
分类导航