Three ways to copy string str1 to string str2 _c language

Source: Internet
Author: User
1. Write your own function and copy two strings
Copy Code code as follows:

#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!= ';p 1++,p2++) {
*P2=*P1;
}
*p2= ' ";
}

2. strcpy function with function library weight
Copy Code code as follows:

#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. Define two string variables, then assign directly
Copy Code code as follows:

#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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.