雷火电竞-中国电竞赛事及体育赛事平台

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

將字符串str1復(fù)制為字符串str2的三種解決方法

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊:

1.自己編寫函數(shù),將兩個字符串進(jìn)行復(fù)制

復(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ù)庫重的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.定義兩個字符串變量,然后直接進(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;
}

上一篇:C語言中system()函數(shù)的用法總結(jié)

欄    目:C語言

下一篇:C++用指針變量作為函數(shù)的參數(shù)接受數(shù)組的值的問題詳細(xì)總結(jié)

本文標(biāo)題:將字符串str1復(fù)制為字符串str2的三種解決方法

本文地址:http://www.jygsgssxh.com/a1/Cyuyan/3943.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有