#includestdio.h#includestdlib.h#includestring.h#defineOK1#defineERROR0#defineOVERFLOW-2typedefintStatus;typedefstruct{char*ch;intlength;}HString;//初始化操作voidInitString(HString*T){T-length=0;T-ch=NULL;}//赋值操作StatusStrAssign(HString*T,char*chars){//生成一个其值等于串常量chars的串Tinti,j;//char*c;if(T-ch)free(T-ch);//释放T原有空间//for(i=0,c=chars;*c;++i,++c);//求chars的长度i,*c表*c!=NULLi=strlen(chars);if(!i){T-ch=NULL;T-length=0;}else{T-ch=(char*)malloc(i*sizeof(char));if(!T-ch)exit(OVERFLOW);for(j=0;ji;j++)T-ch[j]=chars[j];T-length=i;}returnOK;}//StrAssign//串复制操作StatusStrCopy(HString*T,HStringS){inti;if(T-ch)free(T-ch);T-ch=(char*)malloc(S.length*sizeof(char));if(!T-ch)exit(OVERFLOW);for(i=0;iS.length;i++)T-ch[i]=S.ch[i];T-length=S.length;returnOK;}//StrCopy//判空操作StatusStrEmpty(HStringS){if(S.length==0&&S.ch==NULL)returnOK;elsereturnERROR;}//StrEmpty//求长度操作intStrLength(HStringS){returnS.length;}//Strlength//串比较操作intStrCompare(HStringS,HStringT){inti;for(i=0;iS.length&&iT.length;i++){if(S.ch[i]!=T.ch[i])returnS.ch[i]-T.ch[i];}returnS.length-T.length;}//StrCompare//清空操作StatusClearString(HString*S){if(S-ch){free(S-ch);S-ch=NULL;}S-length=0;returnOK;}//ClearString//串连接操作StatusConcat(HString*T,HStringS1,HStringS2){inti;if(T-ch)free(T-ch);//释放旧空间T-ch=(char*)malloc((S1.length+S2.length)*sizeof(char));if(!T-ch)exit(OVERFLOW);for(i=0;iS1.length;i++){T-ch[i]=S1.ch[i];}T-length=S1.length+S2.length;for(i=0;iS2.length;i++){T-ch[S1.length+i]=S2.ch[i];}returnOK;}//Concat//求子串操作StatusSubString(HString*Sub,HStringS,intpos,intlen){inti;if(pos1||posS.length||len0||lenS.length-pos+1)returnERROR;if(Sub-ch)free(Sub-ch);//释放旧空间if(!len){//空子串Sub-ch=NULL;Sub-length=0;}else{//完整子串Sub-ch=(char*)malloc(len*sizeof(char));if(!Sub-ch)exit(OVERFLOW);for(i=0;i=len-1;i++)Sub-ch[i]=S.ch[pos-1+i];Sub-length=len;}returnOK;}//SubString//求子串位置操作intIndex(HStringS,HStringT,intpos){//T为非空串。若主串S中第pos个字符之后存在与T相等的子串,//则返回第一个这样的子串在S中的位置,否则返回0inti,m,n;HStringSub;InitString(&Sub);//一定要初始化Sub!!!if(pos0){n=StrLength(S);m=StrLength(T);i=pos;while(i=n-m+1){//一旦in-m+1,则找不出长度为m的子串SubString(&Sub,S,i,m);if(StrCompare(Sub,T)!=0)i++;//从位置i起取子串与T作比较elsereturni;}//while}//ifreturn0;}//Index//插入操作StatusStrInsert(HString*S,intpos,HStringT){//1≤pos≤StrLength(S)+1。在串S的第pos个字符之前插入串Tinti;if(pos1||posS-length+1)returnERROR;if(T.length){S-ch=(char*)realloc(S-ch,(S-length+T.length)*sizeof(char));if(!S-ch)exit(OVERFLOW);for(i=S-length;i=pos-1;--i)//为插入T而腾出位置S-ch[i+T.length]=S-ch[i];for(i=0;iT.length;i++)S-ch[pos-1+i]=T.ch[i];//插入TS-length+=T.length;}returnOK;}//StrInsert//删除操作StatusStrDelete(HString*S,intpos,intlen){inti;if(S-lengthpos+len-1)returnERROR;for(i=pos-1;i=S-length-len;i++)S-ch[i]=S-ch[i+len];S-length-=len;S-ch=(char*)realloc(S-ch,S-length*sizeof(char));returnOK;}//StrDelete//输出操作voidStrPrint(HStringT){inti;for(i=0;iT.length;i++)printf(%c,T.ch[i]);printf(\n);}//StrPrintvoidmain(){intpos,len;chara[10],b[10];HStringS1,S2,S3,T,Sub;;InitString(&S1);InitString(&S2);InitString(&T);InitString(&S3);InitString(&Sub);printf(输入两个串分别赋值给S1,S2(串长均小于10):\n);scanf(%s%s,a,b);if(StrAssign(&S1,a)&&StrAssign(&S2,b)){printf(\n由串S1复制得串T:);StrCopy(&T,S1);StrPrint(T);if(StrEmpty(T)==1)printf(串T为空!\n);elseprintf(串T非空!\n);printf(\n比较串S1,S2:\n);if(StrCompare(S1,S2)0)printf(S1S2\n);elseif(StrCompare(S1,S2)0)printf(S1S2\n);elseprintf(S1=S2\n);printf(\n连接串S1,S2得串S3:);if(Concat(&S3,S1,S2)){StrPrint(S3);}elseprintf(串连接失败!);printf(串S3的长度为%d:\n,StrLength(S3));printf(\n用Sub返回串S3中第pos个字符起长度为len的字符:\n);printf(输入pos,len的值:\n);scanf(%d%d,&pos,&len);if(SubString(&Sub,S3,pos,len)==1){printf(输出串Sub:);StrPrint(Sub);}elseprintf(序号不合法,取子串失败!\n);printf(\n求串Sub在串S3中的位置:\n);if(Index(S3,Sub,pos)==0)printf(pos值不合法!\n);elseprintf(pos值为%d\n,Index(S3,Sub,pos));printf(\n在串S1的第pos位置插入串S2\n);printf(输入pos的值:\n);scanf(%d,&pos);if(StrInsert(&S1,pos,S2)==1){printf(输出串S1:);StrPrint(S1);}elseprintf(插入失败!\n);printf(\n从串S1中删除第pos位置起长度为len的字符:\n);printf(输入pos,len的值:\n);scanf(%d%d,&pos,&len);if(StrDelete(&S1,pos,len)){printf(输出串S1:);StrPrint(S1);}elseprintf(删除失败!\n);}}