1.将两个字符数组交换(字符数组)
#include<iostream>
#include<string.h>
using namespace std;
void swap(char *p1,char *p2){
char *temp = new char[10];
strcpy(temp,p1);
strcpy(p1,p2);
strcpy(p2,temp);
}
int main(){
char c1[10] = "Chian",c2[10] = "Korea";
cout<<c1<<" "<<c2<<"\n";
char *p1 = c1,*p2 = c2;
swap(p1,p2);
cout<<c1<<" "<<c2;
return 0;
}
2.将两个字符串交换(字符串)
#include<iostream>
using namespace std;
void swap(string *p1,string *p2){
string temp ;
temp = *p1;
*p1 = *p2;
*p2 = temp;
}
int main(){
string str1 = "China",str2 = "Korea";
string *p1 = &str1,*p2 = &str2;
cout<<str1<<" "<<str2<<"\n";
swap(p1,p2);
cout<<str1<<" "<<str2;
return 0;
}