资源预览内容
第1页 / 共12页
第2页 / 共12页
第3页 / 共12页
第4页 / 共12页
第5页 / 共12页
第6页 / 共12页
第7页 / 共12页
第8页 / 共12页
第9页 / 共12页
第10页 / 共12页
亲,该文档总共12页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
Chap4 StringOverviewStringImplementation of StringBasic operationsStrings pattern matching algorithmStringString is a character sequence composing zero or several characters;Strings general form is: s=“s1s2s3sn”;si(1in) is a random character called the element of the string, i is sis position number in the whole string;We call the special string of size 0 is an empty string, marked by .StringWe call any sub-sequence composing of several consecutive characters a substring;The position of a substring in the whole string is the position of its first characters position in the whole string;When two strings have equal size and the same character in the corresponding position, we deem the two strings are equal.Implementation of StringArray Implementation 1 #define MAXSIZE 256 typedef struct char SMAXSIZE; int curlen; SeqString;Implementation of StringArray Implementation 2 The string is ended up by a specific character 0. Implementation of StringLinked List Implementation The string is stored into a single linked list. Though it is feasible, it will bring space waste. So we sometimes store a substring in one node of the linked list, its called block-storage. Basic OperationsStrLength(s): returns the length of the stringStrAssign(s1, s2): copy s2 to s1StrConcat(s1, s2): connect s2 in the end of s1SubStr(s, i, len): get a substring of size len from the position i of sCmpStr(s1, s2): compare two strings s1, s2StrIndex(s, t): find the position of string t in the string s Basic OperationsStrInsert(s, i, t): insert t into s on the position i of tStrDelete(s, i, len): delete a substring of size len from the position of string sStrRep(s, t, r): replace any string t appeared in the string s by string r Pattern MatchingStrings pattern matching is a very important string operation;The procedure of finding substring t in the given string s is called pattern matching;When found, the routine returns the first matching position. If not found, returns 0. Example 4.1Simple pattern matching algorithmint StrIndex_BFint StrIndex_BF(char *s, char *t)(char *s, char *t) int i=1,j=1; int i=1,j=1; while (i=s0 & j=t0 ) while (i=s0 & jt0) if (jt0) return (i-t0); return (i-t0); else else return 0; return 0;
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号