#include <iostream>
#include<string.h>
using namespace std;
int main()
{
char str1[11],str2[11];
char str3[]="hello";
char str4[]="GoodBye";
strcpy(str1,str3); //调用strcpy函数,将str3的内容复制给str1;
strcpy(str2,str4);
int ch=strcmp(str4,str3); //比较两个字符串
int len=strlen(str4); //字符串的长度
strcat(str4,str3); //将两个字符串连在一起
cout<<"str2="<<str2<<endl;
cout<<"str1="<<str1<<endl;
cout<<"str4="<<str4<<endl;
cout<<"len="<<len<<endl;
if(ch==0)
{
cout<<"str4"<<"等于"<<"str3"<<endl;
}
else if(ch>=0)
{
cout<<"str4"<<"大于"<<"str3"<<endl;
}
else if(ch<=0)
{
cout<<"str4"<<"小于"<<"str3"<<endl;
}
return 0;
}
样例:

本文深入探讨了C++中字符串的复制、比较、连接及长度获取等基本操作,通过实例展示了strcpy、strcmp、strcat和strlen函数的使用方法,是初学者理解C++字符串处理的实用指南。
2848

被折叠的 条评论
为什么被折叠?



