First, the basic concept of string
1. Definition A
string is a finite sequence of zero or more characters. Generally denoted as
s= "a1a2a3...an"
where s is the name of the string, and the character sequence enclosed in double quotes is the value of the string; an can be letters, numbers or other characters; the number of characters in the string n is called the string length. A string of zero characters is called an empty string, and its length is zero.
2. Basic concepts :
space string : a string consisting of one or more spaces is called a space string;
(the difference between an empty string and a space string: the length of an empty string is 0, and the length of a space string is not zero.)
Substring : string The sub-sequence composed of any consecutive characters in the
main string is called the sub-string of the string; main string : the string containing the sub-string is called the main string;
the position of the sub-string in the main string: the position where it first appeared in the main string
For example:
main string S="abcdefbcdh"
substring s="bcd"
then the position (or serial number) of s in S is: 2
Two, string comparison
(1) The condition for two strings to be equal: if and only if the string length is equal, and the characters in each position are equal
(2) The comparison of two strings A and B, compare the ASCII codes at the corresponding positions one by one from the front to the back:
① "=" Two strings end at the same time, which means A=B;
for example: "abc"="abc"
② ">" The ASCII code in A is greater than the ASCII code in the corresponding position in B, or B ends, which means A>B
For example: "abxy">"ab" "132">"123"
③ "<"The ASCII code in B is greater than the ASCII code in the corresponding position in A, or A ends, which means A<B
For example: "abc"<"abcd ""Bef"<"c"
Three, the storage representation of the string
Sequential storage : use a group of continuous storage units to store the character sequence in the string
1, fixed-length sequential storage representation
Type definition
# define maxlen 255
typedef struct {
char ch[maxlen]; //存储串的一维数组
int length; //串长度
}sstring;
2. Heap allocation storage representation (dynamic)
type definition
typedef char *string
typedef struct{
char *ch;
int length;
}hstring;
hstring S;
Chain storage : use a singly linked list to store string values.
Type definition
# define nodesize 80
typedef struct node{
char data[nodesize];
struct node *next;
}lsring;