Data structure, data structure and Algorithm
What is the concept of a string?
String: a finite sequence composed of zero or multiple characters. As: s = 'a1a2... An '(n ≥ 0)
S is the string name, 'a1a2... An 'is the string value, and n is the length of the string.
Position of the substring in the primary string: expressed by the position of the first character in the primary string.
Equal strings: if and only when the values of the two strings are equal (the two strings are of the same length and each character is the same
String Data Type
ADT String {D = {ai | ai ε CharacterSet, I = 1, 2 ,..., n, n ≥ 0} R = {<ai-1, ai> | ai-1, ai, D, I = 2 ,..., n} basic operations :......}
Where D is the data object R is the Data Relationship
A string is a finite length Character Sequence enclosed by a pair of single quotes, such as "a string"
String operations
int Index (String S, String T, int pos) { if (pos > 0) { n = StrLength(S); m = StrLength(T); i = pos; while ( i <= n-m+1) { SubString (sub, S, i, m); if(StrCompare(sub,T) != 0) ++i; else return i; } } return 0; }
If return 0, it means that there is no substring equal to T in S.
T is a non-empty string. If there is a substring equal to T after the second pos character in the main string S, the position of the first substring in S is returned; otherwise, 0 is returned.
String operation example (*)
1. SubString (& sub, S, pos, len)
SubString (sub, "commander", 4, 3)
Obtain sub =?
'Man'
Take 3 elements from the first element of "commander"
SubString (sub, commander, 1, 9)
Obtain sub =?
'Commander'
Obtain 9 elements starting from the 1st elements of "commander"
SubString (sub, "commander", 9, 1)
Obtain sub =?
'R'
Obtain 1 element from the first element of "commander ".
SubString (sub, commander, 4, 7)
Obtain sub =?
'Mander'
SubString (sub, "beijing, 7, 2)
Obtain sub =?
'G'
Obtain 1 element from the first element of "commander ".
SubString (sub, "student", 5, 0)
Obtain sub =?
''
Summary
The logical structure of a string is very similar to that of a linear table. The difference is that the data object constraints of a string are character sets.
The basic operations of strings are very different from those of linear tables:
1. In the basic operations of a linear table, most of them use a single element as the operation object.
2. In the basic operations of a string, the "whole string" is usually used as the operation object.