标签:ring res false 复制 acdb == 顺序串 比较 index
#include<stdio.h> #define MaxSize 255 typedef struct { char ch[MaxSize]; int length; }SString; void InitStr(SString &S) { S.ch[0]=‘¥‘; S.length=0; } void StrCreate(SString &S,char a[]) //1.赋值操作 { int i=0,j=1; while(a[i]!=‘\0‘) { S.ch[j++]=a[i++]; } S.length=i; } void StrCopy(SString &S,SString T) //2.复制操作 { for(int i=1;i<T.length;i++) S.ch[i]=T.ch[i]; S.length=T.length; } bool StrEmpty(SString S) //3.判空操作 { return S.length==0; } int StrLen(SString S) //4.求串长 { return S.length; } void ClearStr(SString &S) //5.清空操作 { S.length=0; } void Concat(SString &T,SString S1,SString S2) //6.串链接 { int i; for(i=1;i<=S1.length;i++) //将串S1复制到T T.ch[i]=S1.ch[i]; for(i=1;i<=S2.length;i++) //将S2复制到T尾部 T.ch[S1.length+i]=S2.ch[i]; T.length=S1.length+S2.length; } bool SubString(SString S,SString &Sub,int front,int len) //7.求子串 { if(S.length<front+len-1) //判断子串是否越界 return false; for(int i=1;i<=len;i++) Sub.ch[i]=S.ch[front++]; Sub.length=len; return true; } int CompareStr(SString S,SString T) //8.比较操作 { int i=1; while(i<S.length && i<T.length && S.ch[i]==T.ch[i]) i++; if(S.ch[i]!=T.ch[i]) return S.ch[i]-T.ch[i]; //S>T 返回值>0;S<T 返回值<0;S=T 返回值=0 return S.length-T.length; //扫描过的字符一致,长串大 } int Index(SString S,SString T) //9.定位操作 { int i=1,m=S.length,n=T.length; SString Sub; while(i<m-n+1) //长度为T.length的子串个数 { SubString(S,Sub,i,T.length); //依次求子串 if(CompareStr(Sub,T)==0) //判断两串是否相等 return i; //返回位置i i++; } return 0; } void PrintStr(SString S) //10.输出操作 { printf("the Strings are:"); for(int i=1;i<=S.length;i++) printf("%c\t",S.ch[i]); printf("\n"); } int main() { SString S; InitStr(S); char a[]="abcdefg"; printf("The Strings is %s\n",StrEmpty(S)?"空":"非空"); StrCreate(S,a); printf("The Strings is %s\n",StrEmpty(S)?"空":"非空"); PrintStr(S); printf("The S‘s length is %d\n",S.length); SString Sub; SubString(S,Sub,2,3); PrintStr(Sub); SString T; Concat(T,S,Sub); PrintStr(T); printf("The T‘s length is %d\n",T.length); int i=CompareStr(S,T); if(i==1) printf("The max is %c\n",‘S‘); else if(i==0) printf("The max is S and T\n"); else printf("The max is %c\n",‘T‘); printf("The Sub is Num %d in S\n",Index(S,Sub)); char b[]="acdbg"; SString R; StrCreate(R,b); printf("The Sub is Num %d in S\n",Index(S,R)); ClearStr(T); printf("The Strings is %s\n",StrEmpty(T)?"空":"非空"); return 0; }
标签:ring res false 复制 acdb == 顺序串 比较 index
原文地址:https://www.cnblogs.com/-slz-2/p/13289924.html