Data structure-storage representation and implementation of strings

Source: Internet
Author: User

The basic concept of a string

String: Applied in non-numeric processing, transaction processing and other fields.
Computer hardware: mainly reflect the requirements of numerical calculation.
The processing of strings is more complex than the specific numeric processing.
String: A finite sequence consisting of 0 or more characters. S= "A1a2a3 ...", where S is a string name, and AI (1≦i≦n) is a single character, which can be a letter, number, or other character.

串值:双引号括起来的字符序列,引号不属于串的内容。串长:串中所包含的字符个数。 空串(空的字符串):长度为零的串,它不包含任何字符。空格串(空白串):构成串的所有字符都是空格。 注意:空串和空白串的不同,空格串是有内容有长度的,而且可以不止一个空格;例如“   ”和“”分别表示长度为3的空白串和长度为0的空串。

SUBSTRING (substring): a subsequence of any contiguous character in a string is called a substring of that string, and the string containing the substring is referred to as the main string.

子串的序号:子串在主串中首次出现时,该子串的首字符对应在主串中的序号,称为子串在主串中的序号(或位置)。

For example, a= "This was A string", b= "is",
B is a substring of a, A is the main string.
b The ordinal number in a is 3.
In particular, an empty string is a substring of any string, and any string is its own substring.

Basic operation of the string

For the basic operation of strings, many high-level languages provide the appropriate operations or standard library functions to implement. Here are some of the common string operations in C language.
Define the following variables:

     char s1[20]=“dirtreeformat”;     char s2[20]=“file.mem”;     char s3[30], *p;     int result;
(1)求串长(length)       intstrlen(char *s);  //求串的长度    例如:printf(“%d”,strlen(s1)); 输出13
(2)串复制(copy)      char *strcpy(char *to,char *from);     该函数将串from复制到串to中,并且返回一个指向串to的开始处的指针。  例如:strcpy(s3,s1);   //s3=“dirtreeformat”
(3)联接(concatenation)    charstrcat(char *to,char *from)   该函数将串from复制到串to的末尾,并且返回一个指向串to的开始处的指针。 例如:strcat(s3,”/”);            strcat(s3,s2);  //s3=“dirtreeformat/file.mem”
 (4 ) string comparison (Compare) int  strcmp  (char  *s1,<   Span class= "Hljs-keyword" >char  *s2); The function compares the size of the string S1 and the string S2 when the return value <0 , =0  or >0 , respectively, represents S1<S2, S1=s2, or S1>S2, for example: Result=strcmp  ("Baker", "Baker") Result>0  result=strcmp  ("12  "," 12  ");  Result=0  result=strcmp  ("Joe", "Joseph"); Result<0   
(5)字符定位(index)     int strchr(charchar c);    该函数是找c在字符串中第一次出现的位置,若找到则返回该位置,否则返回NULL。
Storage representation and implementation of a string
串是一种特殊的线性表,其存储表示和线性表类似,但又不完全相同。串在计算机中有3种表示方式:

Fixed-length (static) sequential storage representation: Defines a string as a character array, with a string name to directly access the string value. In this way, the storage space of a string is determined at compile time, and its size cannot be changed.
Heap allocation (dynamic order) storage: The sequence of characters in the string is stored sequentially with a contiguous set of storage units, but the string's storage space is dynamically allocated according to the actual length of the string at the time the program is run.
Block chain storage: is a chain-store structure representation.

Fixed-length sequential storage representation of strings

This storage structure is also known as the sequential storage structure of strings. The so-called fixed-length sequential storage structure is defined directly using a fixed-length character array, and the upper bounds of the array are predetermined.
The fixed-length sequential storage structure is defined as:

#define max_strlen 256  typedef struct  {char  str  [Max_strlen]; int  length;}   StringType; 
1  string junction operation status Strconcat (StringType *s, StringType t) Span class= "Hljs-comment" >/* after the string T is joined to the string s, the result is still saved in S */ {int  i, J; if  ((S->length  +t-> Length ) >max_strlen) return  ERROR; /* coupling length out of range */ for  (I=0 ; I<t->length ; i++) s-> str[s- >length  +i]=t->str[i]; /* string T junction to string S */ s->length  = S->length  + t->length ; /* modify the string length after the junction */ return  OK;} 
1String junction operation Status Strconcat (HString *t, HString s1, HString S2)/* Returns a string that is connected by S1 and S2 with T */{intK, J, T_len;if(t.ch) free (T);/ * Free old space * /T->length=s1.length+s2.length;if((T->ch= (char) malloc (sizeof (*t_len)) ==null) {printf ("system space is not enough, application space failed!") \ n ");returnERROR; } for(j=0; J<s1.length; J + +) T->ch[j]=s1.ch[j];/ * Copy the string S1 to the string T * / for(J=S1.length, k=0; K<s2.length; k++, J + +) T->ch[j]=s2.ch[k];/ * Copy the String s2 to the string T * /returnOK; }
Pattern matching algorithm for strings

Pattern matching (exemplary match): The positioning of substrings in the main string is called pattern matching or string matching. Pattern matching success refers to the ability to find the pattern string T in the main string s, otherwise, the called pattern string T does not exist in the main string s.
Pattern matching is very widely used. For example, in search engines and text-editing programs, we often look for a specific word in the text where it appears. Obviously, the effective algorithm to solve this problem can greatly improve the response performance of the text editing program.
Pattern matching is a more complicated process of string operation. So far, people have put forward many computer algorithms which have different ideas and efficiency in the pattern matching of strings.

Data structure-the storage representation and implementation of a string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.