flag;k++)
{
flag=str1->pData[k]-str2->pData[k];//把串1中元素减串2中的元素结果赋给flag
}
if(!
flag)
{
returnflag;
}else
{
returnflag>0?
1:
-1;
}
}
//子串定位:
若substr在str中出现,则返回第1次出现的位置;否则返回-1
intSubstringLocate(SeqString*str,SeqString*substr)
{
intflag=-1,i,j;
if(!
str||!
substr||str->lengthlength)
{
returnflag;
}
for(i=0;ilength-substr->length;i++)//判断两串的长度是否相等
{
for(j=0;jlength;j++)
{
if(str->pData[i+j]-substr->pData[j])//判断两串的对应元素是否相等
{
break;
}
}
if(j==substr->length)
{
returni;
}
}
return-1;
}
//串插入
intSubstringInsert(SeqString*L,intindex,SeqString*str)
{
intk=0;
SeqString*substr=NULL;//为的是可以串自身插入到自身
if(!
L||!
L->pData||!
str||!
str->pData||index<0
||L->length+str->length>MAX_SIZE)
{
returnERROR;
}
substr=InitString(substr);//初始化新串
CopyFromSeqString(substr,str);//把串复制
for(k=L->length;k>=index;k--)
{
L->pData[k+substr->length]=L->pData[k];//先移动L中index起所有后面的元素
}
//把子串插入
for(k=0;klength;k++)
{
L->pData[index+k]=substr->pData[k];
}
L->length+=str->length;//新串的长度是两个串的和
DestroyString(substr);//销毁串substr
substr=NULL;
returnOK;
}
//子串删除
intSubstringDelete(SeqString*L,intindex,intlen)
{
intk=0;
if(index<0||len<=0||index+len>L->length)
{
printf("参数不合法\n");
returnERROR;
}
for(k=0;k<=L->length-(index+len);k++)
{
L->pData[index+k]=L->pData[index+len+k];//把要删除的子串前移
}
L->length-=len;//串长度减len
returnOK;
}
//串替换
intSubstringSubstitute(SeqString*L,intindex,SeqString*str)
{
intk=0;
SeqString*substr=NULL;
if(!
L||!
L->pData||!
str||!
str->pData||index<0
||index+str->length>MAX_SIZE)
{
returnERROR;
}
substr=InitString(substr);//初始化串
CopyFromSeqString(substr,str);//复制串
for(k=0;k<=substr->length;k++)
{
L->pData[index+k]=substr->pData[k];//把串中的元素
}
L->length=index+str->length;
DestroyString(substr);
substr=NULL;
returnOK;
}
//比较两个字串是否相等
intIsEqual(SeqString*str1,SeqString*str2)
{
intk=0;
if(!
str1||!
str1->pData||!
str2||!
str2->pData)
{
return0;//若有一个字符串不存在,返回0
}
if(str1->length!
=str2->length)
{
return0;//若长度不相等,返回0
}
while(klength&&!
(str1->pData[k]-str2->pData[k]))
{
k++;//比较相应字符
}
if(k==str2->length)
{
return1;
}else{
return0;
}
}
voidmain()
{
SeqString*L=NULL,*des=NULL,*subL=NULL;
L=InitString(L);//初始化顺序串
des=InitString(des);
subL=InitString(subL);
printf("复制串abcd:
");
CopyFromChars(des,"abcd");
PrintString(des);
printf("复制串abc:
");
CopyFromChars(L,"abc");
PrintString(L);
printf("判断两串是否相等:
");
printf("%d\n",IsEqual(L,des));
printf("复制串abcdefgh:
");
CopyFromChars(L,"abcdefgh");
PrintString(L);//打印顺序串
printf("截取子串%s\n",*Substring(L,4,2));
printf("子串位置:
%d\n",SubstringLocate(L,Substring(L,4,2)));
SubstringSubstitute(L,3,L);
PrintString(L);//打印顺序串
SubstringInsert(L,9,L);
PrintString(L);//打印顺序串
SubstringDelete(L,1,2);
PrintString(L);//打印顺序串
subL=Substring(L,1,4);
printf("比较大小%d\n",StringCompare(subL,L));
PrintString(subL);
CopyFromSeqString(des,L);
PrintString(des);
printf("比较大小%d\n",StringCompare(des,L));
ConnectStrings(des,L);//顺序的连接
PrintString(des);
ReverseString(L);
PrintString(L);//打印顺序串
printf("%d\n",GetPosition(L,10));
printf("%d\n",GetStringLength(L));
if(IsEmpty(L)){
printf("顺序串是空的\n");
}else{
printf("顺序串是非空的\n");
}
SetEmpty(L);//把顺序串置空
PrintString(L);//打印顺序串
DestroyString(L);//不要忘了
PrintString(L);//打印顺序串
DestroyString(des);//销毁顺序串
PrintString(des);//打印顺序串
DestroyString(subL);//销毁顺序串
PrintString(subL);//打印顺序串
if(!
L)
free(L);
if(!
des)
free(des);
}