串的基本定义
串是一种特殊的线性表,串中每个数据元素都是一个单字符,对串进行操作时,经常将若干字符作为一个整体进行处理。
S是串名,引号不属于串的内容。
基本操作
①串赋值StrAssign(SqString &S1,SqString $S2)
②求串长StrLength(SqString S)
③串比较StrCompare(SqString S1,SqString S2)
④串连接StrConcat(SqString &S1,SqString S2)
⑤求子串SubString(SqString S,SqString &T,start,len)
⑥子串定位Index(SqString S,SqString T,start)
⑦串插入StrInsert(SqString &S,start,SqString T)
⑧串删除StrDelete(SqString &S,start,len)
⑨串替换StrReplace(SqString &S,SqString T,SqString V)
串的静态顺序存储结构
代码实现
#include <iostream>
#include <cstdlib>
using namespace std;
#define MAXLEN 250
typedef struct {
char elem[MAXLEN];
int length;
}SqString;
void InitStr(SqString &S){
S.length=0;
}
void SetString(SqString &S){
char ch;
cout<<"输入串:"<<endl;
while((ch=getchar())!='\n'){
if(S.length<MAXLEN)
S.elem[S.length++]=ch;
else{
cout<<"空间已满!"<<endl;
return ;
}
}
}
void StrAssign(SqString &S1,SqString S2){
for(int i= 0;i<S2.length;i++)
S1.elem[i]=S2.elem[i];
S1.length=S2.length;
}
int StrLength(SqString S){
return S.length;
}
int StrCompare(SqString S1,SqString S2){
int minn=S1.length<S2.length?S1.length:S2.length;
for(int i=0;i<minn;i++)
if(S1.elem[i]>S2.elem[i])
return 1;
else if(S1.elem[i]<S2.elem[i])
return -1;
else continue;
if(S1.length==S2.length)
return 0;
else if(S1.length>minn)
return 1;
else return -1;
}
int StrConcat(SqString &S1,SqString S2){
if(S1.length+S2.length<=MAXLEN){
for(int i=0;i<S2.length;i++)
S1.elem[S1.length+i]=S2.elem[i];
S1.length+=S2.length;
return 1;
}
else{
cout<<"空间不足!"<<endl;
return 0;
}
}
void StrString(SqString S,SqString &T,int start,int len){
}