將字符串str1復(fù)制為字符串str2的三種解決方法
更新時(shí)間:2013年10月17日 08:37:57 作者:
以下是對(duì)將字符串str1復(fù)制為字符串str2的三種解決方法進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助
1.自己編寫函數(shù),將兩個(gè)字符串進(jìn)行復(fù)制
#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!='\0';p1++,p2++){
*p2=*p1;
}
*p2='\0';
}
2.使用函數(shù)庫(kù)重的strcpy函數(shù)
#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.定義兩個(gè)字符串變量,然后直接進(jìn)行賦值
#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;
}
復(fù)制代碼 代碼如下:
#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!='\0';p1++,p2++){
*p2=*p1;
}
*p2='\0';
}
2.使用函數(shù)庫(kù)重的strcpy函數(shù)
復(fù)制代碼 代碼如下:
#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.定義兩個(gè)字符串變量,然后直接進(jìn)行賦值
復(fù)制代碼 代碼如下:
#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;
}
相關(guān)文章
解析Linux內(nèi)核的基本的模塊管理與時(shí)間管理操作
這篇文章主要介紹了Linux內(nèi)核的基本的模塊管理與時(shí)間管理操作,包括模塊加載卸載函數(shù)的使用和定時(shí)器的用法等知識(shí),需要的朋友可以參考下2016-02-02使用C# 判斷給定大數(shù)是否為質(zhì)數(shù)的詳解
本篇文章是對(duì)使用C#判斷給定大數(shù)是否為質(zhì)數(shù)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C++順序容器(vector、deque、list)的使用詳解
本文主要介紹了C++順序容器(vector、deque、list)的使用詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06C++右值引用與移動(dòng)構(gòu)造函數(shù)基礎(chǔ)與應(yīng)用詳解
左值和右值都是針對(duì)表達(dá)式,左值是指表達(dá)式結(jié)束后依然存在的持久對(duì)象,右值是指表達(dá)式結(jié)束時(shí)就不再存在的臨時(shí)對(duì)象,下面這篇文章主要給大家介紹了關(guān)于C++11右值引用和移動(dòng)語(yǔ)義的相關(guān)資料,需要的朋友可以參考下2023-02-02C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之平衡二叉樹(AVL樹)實(shí)現(xiàn)方法示例
這篇文章主要介紹了C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之平衡二叉樹(AVL樹)實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了C語(yǔ)言平衡二叉樹的相關(guān)定義與使用技巧,需要的朋友可以參考下2018-01-01