中国空间站结构示意图上海中小企业发展服务中心
C-串以一个全0字节结束字符序列,C-串的空间长度为字符串长度加1。
char buffer[7]=”hello!”;注意是7而非6;
C-串的类型为char*,也就是const char*。Char*称为字符指针,表示C-串的起始地址。
如;
char *str="Hello";
cout<<*str<<endl; //显示H
cout<<str<<endl; //显示Hello//======================
//C-串操作
//======================
#include<iostream>
 #include<cstring>
 
 using namespace std;
 
 int main()
 {
 char *s1="Hello,world_";
 char *s2="1234_";
 char a[20];
 
 cout<<strcpy(a,s1)<<endl;                                       //复制
 cout<<(strcmp(a,s1)==0 ? "" : " not")<<"enqual\n";  //比较
 cout<<strcat(a, s2)<<endl;                          //连接
 cout<<strrev(a)<<endl;                              //倒置
 cout<<strset(a,'c')<<endl;                         //设置
 cout<<(strstr(s1, "ell") ? "" : "not ")<<"found\n"; //查找串
 cout<<(strchr(s1,'c') ? "" : "not ")<<"found\n";    //查找字符
 }
 
 String是一种自定义类型,比C-串更方便,自动处理空间占用问题,有搜索操作,插入操作,取长度,删除字符,删除字串,判断空串。
 
 
//======================
//string操作
//======================
#include<iostream>
 #include<algorithm>
 
 using namespace std;
 
 int main()
 {
     string a,s1="Hello,world_";
     string s2="1234_";
     a=s1;
     cout<<a<<endl;                                   //复制
     cout<<(a==s1 ? "" : "not")<<"equal\n";             //比较
     cout<<a+s2<<endl;                                  //连接
     reverse(a.begin(), a.end());
      cout<<a<<endl;                                   //倒置串
     cout<<a.replace(0,9,9,'c')<<endl;                  //设置
     cout<<(s1.find('ell')!=-1 ? "" : "not")<<"found\n";//查找串
     cout<<(s1.find('c')!= -1 ? "":"not")<<"found\n";   //查找字符
 
 }
 
 
 
 
