“ Ctrl AC!一起 AC!”
目录
字符串的顺序存储称为顺序串。
结构描述:
#define MAXSIZE 100
typedef struct{
char str[MAXSIZE];
int length;
}seqstring;
插入操作:
void strinsert(seqstring *S,int i,seqstring T){ //将T插入到S从i开始的地方
int k;
if(i<1||i>S->length+1||S->length+T.length>MAXSIZE-1){
printf("cannot insert\n");
}
else{
for(k=S->length-1;k>=i-1;k--){
S->str[T.length+k]=S->str[k];
}
for(k=0;k<T.length;k++){
S->str[i+k-1]=T.str[k];
}
S->length=S->length+T.length;
S->str[S->length]='\0';
}
}
删除操作:
void strdelete(seqstring *S,int i,int len){ //从S的位置i开始删除长度为len的子串
int k;
if(i<1||i>S->length||i+len-1>S->length) printf("cannot delete\n");
else{
for(k=i+len-1;k<S->length;k++){
S->str[k-len]=S->str[k];
}
S->length=S->length-len;
S->str[S->length]='\0';
}
}
连接操作:
seqstring *strconcat(seqstring S1,seqstring S2){ //将S2接到S1的后面
int i;
seqstring *r;
if(S1.length+S2.length>MAXSIZE-1){
printf("cannot concate\n");
return NULL;
}
else{
r=(seqstring*)malloc(sizeof(seqstring));
for(i=0;i<S1.length;i++) r->str[i]=S1.str[i];
for(i=0;i<S2.length;i++) r->str[S1.length+i]=S2.str[i];
r->length=S1.length+S2.length;
r->str[r->length]='\0';
}
return r;
}
求子串操作:
seqstring *substring(seqstring S,int i,int len){ //截取从S的位置i开始的长度为len的子串
int k;
seqstring *r;
if(i<1||i>S.length||i+len-1>S.length){
printf("error\n");
return NULL;
}
else{
r=(seqstring*)malloc(sizeof(seqstring));
for(k=0;k<len;k++) r->str[i]=S.str[i-1+k];
r->length=len;
r->str[r->length]='\0';
}
return r;
}
感谢阅读!!!
“ Ctrl AC!一起 AC!”