字符串库cstring的常见操作
// 字符串拷贝
// strcpy (char dest [],char src []); // 拷贝 src 到dest
// 字符串比较大小
// int strcmp (char s1[],s2[]); // 返回 0则相等
// 求字符串长度
// int strlen (char s[]); // 不算结尾的 '\0'
// 字符串 拼接
// strcat (char s1[],s2[]); //s2 拼接到 s1 后面
// 字符串转成大写
// strupr(char s[]);
// 字符串转成小写
// strlwr(char s[]);
#include<iostream>
#include<cstring>
using namespace std;
// 输出字典序小的字符串
void PrintSmall(char s1[],char s2[]){
if(strcmp(s1,s2)<=0){
cout<<s1<<endl;
}
else{
cout<<s2<<endl;
}
}
int main(){
char s1[40];
char s2[40];
char s3[100];
strcpy(s1,"Hello");
// copy Hello to s1
strcpy(s2,s1);
// copy s2 to s1
cout<<"1)"<<s1<<endl;
strcat(s1,",world");
// catch the ",world" to s1
cout<<"2)"<<s1<<endl;
cout<<"3)";
PrintSmall("abc",s2);
// print the smallest string of dict.
cout<<endl;
cout<<"4)";
PrintSmall("abc","aaa");
cout<<endl;
int n=strlen(s2);
// return a length of string .
cout<<"5)"<<n<<","<<strlen("abc")<<endl;
strupr(s1);
cout<<"6)"<<s1<<endl;
strlwr(s1);
cout<<"7)"<<s1<<endl;
system("pause");
return 0;
}

版权声明:本文为ZripenYe原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。