您好,欢迎访问三七文档
当前位置:首页 > 财经/贸易 > 资产评估/会计 > 数据结构C语言串的基本操作
串的基本操作#includestdio.h#includestdlib.h#includeconio.h#definem100typedefstruct{charch[m];intlength;}Hstr;voidmain(){Hstr*l,*p,*r;charc,w;inth,j,k;inti=0;l=(Hstr*)malloc(sizeof(Hstr));p=(Hstr*)malloc(sizeof(Hstr));r=(Hstr*)malloc(sizeof(Hstr));l-length=0;p-length=0;r-length=0;printf(请选择相关操作(数字1~5控制,输入0结束)\n);printf(--------------1.建立串---------------\n);printf(--------------2.显示串长度-----------\n);printf(--------------3.生成与原来相同的串---\n);printf(--------------4.串比较---------------\n);printf(--------------5.串连接---------------\n);printf(--------------6.返回值---------------\n);scanf(%c,&w);getchar();while(w){switch(w){case'1':{printf(请输入字符(#结束):\n);scanf(%c,&c);while(c!='#'){l-length++;l-ch[i]=c;i++;scanf(%c,&c);}printf(串中字符为\n);for(i=0;il-length;i++)printf(%c,l-ch[i]);printf(\n);}break;case'2':{printf(串长度为%d\n,l-length);}break;case'3':{for(i=0;il-length;i++){p-ch[i]=l-ch[i];}p-length=l-length;printf(复制的串中字符为\n);for(i=0;ip-length;i++)printf(%c,p-ch[i]);printf(\n);}break;case'4':{i=0;printf(请输入要与原串比较的字符串(#结束):\n);scanf(%c,&c);while(c!='#'){r-length++;r-ch[i]=c;i++;scanf(%c,&c);}printf(第二个串中字符为\n);for(i=0;ir-length;i++)printf(%c,r-ch[i]);printf(\n);for(i=0;ir-length&&il-length;i++){if(l-ch[i]!=r-ch[i]){if((l-ch[i]-r-ch[i])0){printf(第二个串大);printf(\n);}if((l-ch[i]-r-ch[i])0){printf(第一个串大);printf(\n);}break;}}if(i==r-length||i==l-length)printf(两个串一样大\n);}break;case'5':{i=0;printf(请输入要与原串连接的串(#结束)\n);scanf(%c,&c);while(c!='#'){r-length++;r-ch[i]=c;i++;scanf(%c,&c);}printf(第二个串中字符为\n);for(i=0;ir-length;i++)printf(%c,r-ch[i]);printf(\n);for(i=0,j=0;ir-length;i++,j++)l-ch[l-length+i]=r-ch[j];l-length=l-length+r-length;printf(连接后第一个串中字符为\n);for(i=0;il-length;i++)printf(%c,l-ch[i]);printf(\n);}break;case'6':{i=0;printf(请输入要找串的起始位置(第几个字符?)\n);scanf(%d,&h);printf(请输入要找的字符个数\n);scanf(%d,&k);printf(内容为:\n);for(i=0;ik;i++){printf(%c,l-ch[h-1]);h++;}printf(\n);}break;}getchar();printf(请选择相关操作(数字1~6控制,输入0结束)\n);printf(--------------1.建立串---------------\n);printf(--------------2.显示串长度-----------\n);printf(--------------3.生成与原来相同的串---\n);printf(--------------4.串比较---------------\n);printf(--------------5.串连接---------------\n);printf(--------------6.返回值---------------\n);scanf(%c,&w);getchar();}}模式匹配#includestdio.h#includestdlib.h#includestring.h#defineboolint#definetrue1#definefalse0typedefstruct{char*ch;intlength;}HString;boolassign(HString*t,char*chs){inti=0;char*c=chs,*d=chs;while(c[i])//c不为空时进入循环{i++;//计算字符串常量chs的长度}if(!i){t-ch=NULL;t-length=0;}else{t-ch=(char*)malloc(i*sizeof(char));c=t-ch;if(!t-ch)//如果没有申请到空间return-1;while(*chs)//chs不为空时进入循环{*c++=*chs++;}t-length=i;}returntrue;}boolinsert(HString*s,intpos,HString*t){if(pos0||poss-length)returnfalse;if(t-length)//t非空{inti=0;if(!(s-ch=(char*)realloc(s-ch,(s-length+t-length)*sizeof(char))))exit(-1);for(i=s-length-1;i=pos;i--)s-ch[i+t-length]=s-ch[i];for(i=0;it-length;i++)s-ch[pos+i]=t-ch[i];s-length+=t-length;}returntrue;}intstrLength(HString*s){returns-length;}intstrCompare(HString*s,HString*t){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;}boolsubString(HString*sub,HString*s,intpos,intlen){if(pos0||pos=s-length||len0||len+poss-length)returnfalse;if(sub-ch)free(sub-ch);if(!len){sub-ch=NULL;sub-length=0;}else{inti=0;sub-ch=(char*)malloc(len*sizeof(char));for(;ilen;i++){sub-ch[i]=s-ch[pos+i];}sub-length=len;}returntrue;}voidprintStr(HString*t){inti=0;while(i++t-length){putchar(t-ch[i-1]);//printf(第%d个:%c\n,i-1,t-ch[i-1]);}putchar('\n');}intindexBF(HString*s,HString*t,intpos){inti=pos,j=0;if(pos0)return-1;while(is-length&&jt-length){if(s-ch[i]==t-ch[j]){i++;j++;}else{i=i-j+1;j=0;}}if(j==t-length)returni-t-length;return-1;}intfindNext(HString*p,int*next){inti=0,j=-1;next[0]=-1;while(ip-length){while(j==-1||ip-length&&p-ch[i]==p-ch[j]){i++;j++;if(p-ch[i]!=p-ch[j])next[i]=j;elsenext[i]=next[j];}j=next[j];}return-1;}voidfindNextString(char*p,int*next){intlen=strlen(p);inti=0,j=-1;next[0]=-1;while(ilen){if(j==-1||p[i]==p[j]){i++;j++;if(p[i]!=p[j])next[i]=j;elsenext[i]=next[j];}else{j=next[j];}}}voidfindNext2(HString*p,int*next){intlen=p-length;char*c=p-ch;inti=0,j=-1;next[0]=-1;while(ip-length){if(j==-1||c[i]==c[j]){i++,j++;if(c[i]!=c[j])next[i]=j;elsenext[i]=next[j];}elsej=next[j];}}intkmpString(char*t,char*p,intpos){inti=pos,j=0,plen=strlen(p),tlen=strlen(t);int*next=(int*)malloc(plen*sizeof(int));findNextString(p,next);while(itlen&&jplen){if(j==-1||p[j]==t[i]){i++;j++;}else{j=next[j];}}if(j==plen)returni-plen;return-1;}intkmp2(HString*t,HString*p,intpos){int*next=(int*)malloc(p-length*sizeof(int));inti=pos,j=0;findNext2(p,next);while(it-length&&jp-length){if(j==-1||p-ch[j]==t-ch[i]){i++,j++;}elsej=next[j];}if(j==p-length)returni-j;return-1;}intkmp(HString*t,HString*p,intpos){int*next=(int*)malloc(p-length*sizeof(int));inti=pos,j=0;findNext(p,next);while(i=t-length-p-length){whil
本文标题:数据结构C语言串的基本操作
链接地址:https://www.777doc.com/doc-5005697 .html