Data Structure and algorithm --- string (bottom)

Source: Internet
Author: User
Tags string methods truncated

The previous two articles introduce the concepts of strings, abstract data types, and KMP pattern matching algorithms. In this article, we will learn some common algorithms of strings.

String-related operation Algorithms

 

Strassign:

/* Function: generate a string whose value is equal to chars T */status strassign (string T, char * chars) {int I; If (chars [0]> maxsize) return Error; t [0] = chars [0]; // chars [0] stores the character chars length t [0] stores the string T Length for (I = 1; I <= chars [0]; I ++) T [I] = * (chars + I-1); // save chars characters to T in sequence and Return OK ;}

The function of this algorithm is to create strings, which is nothing complicated.

 

Strcopy:

/* Function: String t copied by string S */status strcopy (string S, string t) {int I; for (I = 0; I <= s [0]; I ++) T [I] = s [I]; // assigns the characters of the S string to the Return OK ;}

In C #, to copy a string to another string, you only need to call the copy () method of the string class. The specific algorithm of the copy () method is actually demonstrated by the above Code. There is a cloud in the old saying: "You know it, you know it." programming requires training, not just training our coding speed, but understanding programming ideas from programming. Learning classic algorithms is to comprehend programming ideas.

Strempty:

/* Function: determines whether the string is an empty string. True is returned. Otherwise, false */status strempty (string s) {If (s [0] = 0) is returned) // if the character length is 0, return true; else return false ;}

The algorithm used to judge whether the string is null is actually quite simple? Simple algorithms are needed. In fact, algorithms are still difficult, especially some classic algorithms. But don't be discouraged. the joy of life is what we get in constant ways to overcome suffering. Just do simple things, how boring is life?

Strcompare:

/* Function: compare two string operations: If two strings are S> T, the return value is> 0; if S = t, the return value is 0; if S <t, returns <0 */INT strcompare (string S, string t) {int I; for (I = 1; I <= s [0] & I <= T [0]; ++ I) {If (s [I! = T [I]) // determines whether the characters in the two strings are equal. Return s [I]-T [I]; // determines whether the return value is greater than zero, else return s [0]-T [0]; // if the two strings are equal, return 0 }}

This algorithm is used to determine whether two strings are equal.

 

Clearstring:

/* Function: empty string s as an empty string */status clearstring (string s) {s [0] = 0; // set the character length to 0 and the string to an empty string. Return OK ;}

This algorithm is used to clear strings.

 

Strconcat:

/* Function: use t to return a new string joined by S1 and S2. True if not truncated; otherwise false */status Concat (string T, string S1, string S2) {int I; if (S1 [0] + S2 [0] <= maxsize) // not truncated {for (I = 1; I <= S1 [0]; I ++) T [I] = S1 [I]; for (I = 1; I <= S2 [0]; I ++) T [S1 [0] + I] = S2 [I]; t [0] = S1 [0] + S2 [0]; return true ;} else // truncation {for (I = 1; I <= S1 [0]; I ++) T [I] = S1 [I]; for (I = 1; I <= maxsize-S1 [0]; I ++) T [S1 [0] + 1] = S2 [I]; S1 [0] = maxsize; return false ;}}

The function of this algorithm is to concatenate two strings. Note that there may be insufficient space during splicing and the possibility of string truncation. Therefore, the algorithm must be operated by situation.

Let's take a look at the implementation of the substring method. This method must be unique.

/* Function: Use sub to return the initial condition of the string with The LEN Length starting from the POs position in the S string: the string s exists, 1 <= POS <= strlength (s) 0 <= Len <= strlength (S)-pos + 1 */status substring (string sub, string S, int POs, int Len) {int I; if (Pos <1 | POS> S [0] | Len <0 | Len> S [0]-pos + 1) return false; for (I = 1; I <= Len; I ++) sub [I] = s [POS + I-1]; sub [0] = Len; Return OK ;}

 

Index:

/* Function: return the position of string t after the POs position of the Main string S. If it does not exist, the return value is 0. Initial Conditions: String S and T exist, 1 <= POS <= strlength (s) */INT index (string S, string T, int POS) {int I = Pos; // The current position of I Int J = 1; // the current position of the substring t while (I <= s [0]) {If (s [I] = T [J]) // The characters at the current position are equal. Continue matching {++ I; ++ J ;} else // The current position is not equal {j = 1; // J traces back to the first position of the string I = I-j + 2; // The Position of the master string is traced back to the next position where the last matching is successful.} If (j> T [0]) return I-t [0]; else return 0 ;}

In fact, this method is the simple pattern matching algorithm I mentioned in the data structure and algorithm-string (in) article.

 

Let's look at another algorithm that completes matching by calling other string methods:

/* Function: return the position where the sub-string T appears after the POs position in the main string S. If no position exists, the return value is 0. Initial Condition: 1 <= POS <= strlength (s) */INT index2 (string S, string T, int POS) {int I = Pos; int n, m; // n indicates the length of the Main string S, M indicates the length of the substring T if (I> 0) {n = strlength (s); // call strlength () to obtain the length of the Main string s m = strlength (t); // call strlength () to obtain the length of the substring t while (I <= N-m + 1) {substring (sub, S, I, n); // call substring () to obtain sub if (strcompare (T, sub) of N lengths starting from position I) = 0) // call the strcompare method to determine whether the obtained sub-string has the string t equal return I; // if it is equal, return the I-bit else I ++; // if they are not equal, I will go back to the next position} return 0 ;}

By observing this algorithm, we call some of the previously mentioned algorithms. In fact, the basic operations of strings are just a few algorithms. Complex operations require mutual cooperation between methods.

Now, the character string chapter is complete.

Data Structure and algorithm --- string (bottom)

Related Article

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.