1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 int main(){ 5 char a[30] = "LaiXingYu", b[] = "LiuPeiWen", c[] = "LaiXingYu"; 6 cout<<"a: "<<a<<endl<<"b: "<<b<<" c: "<<c<<endl; 7 //按照字典顺序比较字符串,从左到右比较,字母靠前的小 8 cout<<"比较a,b: "<<strcmp(a,b)<<endl<<"比较a,c: "<<strcmp(a, c)<<endl; 9 cout<<"把b复制到a"<<endl; 10 strcpy(a, b); 11 cout<<"a: "<<a<<" b: "<<b<<endl; 12 cout<<"把b的一部分复制到a"<<endl; 13 strcpy(a, &b[5]); 14 cout<<"a: "<<a<<" b: "<<b<<endl; 15 strcpy(a, c); 16 cout<<"字符串连接"<<endl; 17 strcat(a, b); 18 cout<<"a+b: "<<a<<endl; 19 //这里需要注意的是,如果连接后的长度大于a[]的长度, 运行会出现问题, 所以确保a的长度足够长 20 return 0;}