# The fourth chapter: 1. String--the definition of string type and the expression and implementation of string

Source: Internet
Author: User

Objective:

Almost all applications on a computer use string data as the processing object, however, the hardware structure of the computer we are using today is primarily reflective of the need for numerical computation, so it is much more complex to process string data than to process integers and floating-point numbers. Moreover, in different types of applications, the strings handled have different characteristics, in order to effectively implement the processing of strings, it is necessary to use the appropriate storage structure according to the situation. In this chapter, we will discuss some basic string processing operations and several different storage structures.

Directory:

1. Definition of String type

2. Expression and implementation of strings

2.1. Fixed-length sequential storage representation

2.2. Heap Allocation storage representation

2.3. Fast chain storage representation of strings

3. Pattern matching algorithm for strings

4. String operation applied violently

Body:

Definition of String type

A string (string) is a finite sequence of 0 or more characters, which is generally written as:

s= ' a1a2...an '

Note: A string consisting of one or more spaces, called a space string. Instead of an empty string.

The difference between a string and a sequence of characters (char * = ' hello '):

A string is a data structure that is a collection of characters that implements and provides various methods for manipulating such collections.

Char is a basic data type of C and does not have an implemented complex operation on a sequence of characters.

The logical structure of a string is very similar to a linear table, except that:

1. The data object of a string is constrained to a character set.

2. In the basic operation of the linear table, the "Single Data Element" is the object of operation. In the string as a "string of the whole" as an operand, for example: Find substrings, insert and delete substrings.

The expression and realization of string

There are 3 methods of in-machine representation:

1. Fixed-length sequential storage representation

A sequential storage structure similar to a linear table that stores a sequence of characters for a string of values using a contiguous set of storage cells. In the fixed-order storage structure of a string, a fixed-length storage area for each defined string variable is allocated according to the predefined size, and the array can be described as follows.

Storage Representations:

#define MAXSTRLEN 255//define Maximum string length

typedef unsigned char sstring [Maxstrlen +1]; 0 unit length of the storage string

The actual length of the string can be arbitrarily in the range of this predefined length, and the excess part is discarded, called "truncation".

Cons: When merging two strings, if the length exceeds the pre-defined maximum string length Maxstrlen, the rest will be lost as "truncated".

Solution: Use the maximum length of unlimited strings, that is, the storage space for dynamically allocating string values.

2. Heap Allocation storage representation

Feature: A sequence of string-valued characters is still stored in a contiguous set of storage units, but their storage space is allocated dynamically during program execution.

In C, there is a free store called "Heap", which is managed by the C-language dynamic allocation function malloc () and Freedom (). Use the malloc function to allocate a block of storage space for each newly generated string to the actual string length, and if the allocation succeeds, return a pointer to the starting address as the base of the string, and, for ease of handling, the agreed string length is also part of the storage structure.

The heap allocation store represents:

typedef struct {

Char *ch; If the string is not empty, the storage area is allocated by string length, otherwise ch is NULL

int length; String length

}hsring;

Code implementation:

`#include <stdio.h>#include<stdlib.h>#defineTRUE 1#defineFALSE 0#defineOK 1#defineERROR 0#defineINFEASIBLE-1#defineOVERFLOW-2#defineMaxqsize 5//Status is the type of function whose value is the function result status codetypedefintStatus;typedefstruct{    Char*ch;//if the string is not empty, the storage area is allocated by string length, otherwise ch is NULL    intLength//string Length}hstring;//generates a string t whose value equals the string constant charsStatus strassign (HString &s,Char*chars) {    inti;  for(i=0; chars[i];i++){}    //if (s.ch) {//Free (s.ch); //S.ch=null; //}    if(!i) {s.ch=NULL; S.length=0; }Else{s.ch=(Char*)malloc(i*sizeof(Char)); if(!s.ch) exit (OVERFLOW); S.length=i;  for(intj=0; j<i;j++) S.ch[j]=Chars[j]; }    returnOK;}//return string lengthintStrlength (HString &S) {    returns.length;}//Compare size, if s>t return value >0. Returns equal to 0, otherwise returns <0intstrcompare (HString s,hstring T) { for(intI=0; i<s.length&&i<t.length;i++){        if(s.ch[i]!=T.ch[i])returns.ch[i]-T.ch[i]; }    returns.length-t.length;}//empties the string s into empty strings and frees up the occupied spaceStatus clearstring (HString &S) {    if(s.ch) { Free(s.ch); s.ch=NULL; } s.length=0; returnOK;}//concatenate two strings to generate a new stringStatus Concat (HString &t,hstring s1,hstring S2) {    //if (t.ch) free (t.ch);T.ch= (Char*)malloc((s1.length+s2.length) *sizeof(Char)); if(!t.ch) exit (OVERFLOW); T.length=s1.length+s2.length;  for(intI=0; i<s1.length;i++) T.ch[i]=S1.ch[i];  for(i=0; i<s2.length;i++) T.ch[i+s1.length]=S2.ch[i]; returnOK;}//string Intercept, return the intercepted stringStatus SubString (HString &sub,hstring S,intPosintLen) {    if(pos<1|| pos>s.length| | len<0|| (s.length-pos+1) <len)returnERROR; //if (sub.ch) free (sub.ch);Sub.ch= (Char*)malloc(len*sizeof(Char)); if(!sub.ch) exit (OVERFLOW);  for(intI=0; i<len;i++) Sub.ch[i]=s.ch[i+pos-1]; Sub.length=Len; returnOK;}voidPrintv (HString &R) {     for(intI=0; i<s.length;i++) {printf ("Address:%p,",&S.ch[i]); printf ("Value:%c\n", S.ch[i]); }    }voidPrints (HString &R) {     for(intI=0; i<s.length;i++) {printf ("%c", S.ch[i]); } printf ("%s\n"," ");}voidMain () {HString S1; Char*c="Hello";    Strassign (S1,C);    Clearstring (S1); C="Hello";    Strassign (S1,C); printf ("%s","S1:");    Prints (S1);    Printv (S1);    HString S2; C=" China";    Strassign (S2,C); printf ("%s","S2:");    Prints (S2);    Printv (S2);    HString T;    Concat (T,S1,S2); printf ("%s","T:");    Prints (T);    Printv (T);    HString Sub; SubString (Sub,t,6,5); printf ("%s","Sub:");    Prints (sub); Printv (sub);}`

Operation Result:

The fourth chapter: 1. String--the definition of string type and the expression and implementation of string

Related Keywords:

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.