First of all, we must understand the related term of a fixed-length string: String Length: refers to the length of a string, for example, "abcde" is 5 in length, and "PI" is 2 in length equal: the length and content of the two strings are equal, for example, "abcde" and "abcde" Space strings: the string consists of one or more spaces. The space string is not an empty string and has a length, for example, "" empty string: the string length is zero, for example, "" sub-string: a part of a string, for example, "a", "bc" of "abcde ", "bcd" and so on are all substrings [cpp] // ----- header file ------ # include <stdio. h> # include <stdlib. h> # include <stdbool. h> // ------ macro definition ------ # define MAXSTRLEN 255 # define OVERFLOW-2 # define TURE 1 # define ERROR 0 // ------- replace type name -------- typedef unsign Ed char SString [MAXSTRLEN + 1]; typedef int Bool; // ----- function list ----- Bool StrAssign (SString * T, char * s); Bool StrCopy (SString * T, SString S); Bool StrEmpty (SString S); Bool ClearString (SString * S); int StrCompare (SString S, SString T); int StrLength (SString S ); bool Concat (SString * T, SString S1, SString S2); Bool SubString (SString * Sub, SString S, int pos, int len); Bool StrInsert (SString * S, int pos, SString T); Bool StrDelete (SString * S, int pos, int len); Bool Replace (SString * S, SString T, SString V); Bool DestroyString (SString * S ); int Index (SString S, SString T, int pos); void StrOutput (SString S); // ----- test the replacement of the main function ------ int main () {SString t, * T = & t, s, * S = & s, s0, * S0 = & s0; int n; // string initialization StrAssign (T, "abcdehdeklmdepqded "); strAssign (S0, "de"); StrAssign (S, "$"); // string copy StrCopy (T, s0); if (StrEmpty (t) {prin Tf ("yes is empty! \ N ");} else {printf (" non empty! \ N ") ;}// clear string // ClearString (T); if (StrEmpty (t) {printf (" yes is empty! \ N ");} else {printf (" non empty! \ N ") ;}// determine whether two strings are equal n = StrCompare (t, s0); printf (" The reasult is: % d \ n ", n ); // obtain The string length printf ("The length is: % d \ n", StrLength (t); // string connection Concat (S, t, s0 ); strOutput (s); // obtain the specified string SubString (S, t, 5, 5); StrOutput (s); // Insert the string StrInsert (T, 1, s0 ); strOutput (t); // Delete the string StrDelete (T, 5, 5); StrOutput (* T); // Replace (T, s0, s ); strOutput (* T); return 0;} // ------ generate a string whose value is equal to the String constant T ------ Bool StrAssign (SString * T, char * s) {int I; for (I = 0; '\ 0 '! = * S; s ++, I ++) {(* T) [I + 1] = * s;} (* T) [0] = I; // The first address of the string stores the string length return TURE;} // ------ copy the string S to the string T, generate a string T ------ Bool StrCopy (SString * T, SString S) {int I; for (I = 1; I <= S [0]; I ++) {(* T) [I] = S [I];} (* T) [0] = S [0]; return TURE ;} // ----- determines whether it is an empty string. If it is null, true is returned. If it is not null, ERROR ------ Bool StrEmpty (SString S) {return S [0] = 0? TURE: ERROR;} // ----- compare the size of two strings ------ // if the value of string S is greater than the value of string T, 1 is returned,-1 is returned, and 0 is returned, error returned-2 int StrCompare (SString S, SString T) {int I; if (0 = S [0] | 0 = T [0]) {return-2; // ERROR cannot be returned because ERROR = 0} for (I = 1; I <= S [0] & I <= T [0]; I ++) {if (S [I]> T [I]) {return 1;} else if (S [I] <T [I]) {return-1;} else {continue;} if (S [0] = T [0]) {return 0 ;} else if (S [0]> T [0]) {return 1 ;}else {return-1 ;}// ----- return length ----- int StrLen Else (SString S) {return S [0];} // ----- clear string ----- Bool ClearString (SString * S) {if (0! = (* S) [0]) {(* S) [1] = '\ 0'; // write the first address to' \ 0' (* S) [0] = 0; // String Length write 0} return TURE;} // ----- string connection, connect string S2 to S1 and place it in string T ----- Bool Concat (SString * T, SString S1, SString S2) {int I, j; // if (S1 [0] + S2 [0] <= MAXSTRLEN) {for (I = 1; I <= S1 [0]; I ++) {(* T) [I] = S1 [I]; // copy string S1 to string T} for (j = 1; I <= S1 [0] + S2 [0]; I ++) {(* T) [I] = S2 [j ++]; // copy string S2 to string T} (* T) [0] = S1 [0] + S2 [0]; return TURE ;} // In this case, S1 can be completely copied, but S2 can only copy part of it, because this function preferentially replicates S 1 else if (S1 [0] <MAXSTRLEN & (S1 [0] + S2 [0])> MAXSTRLEN) {for (I = 1; I <= S1 [0]; I ++) {(* T) [I] = S1 [I];} for (j = 1; I <= MAXSTRLEN; I ++) {(* T) [I] = S2 [j ++] ;}( * T) [0] = MAXSTRLEN; return ERROR ;} // only a portion of else {for (I = 1; I <= MAXSTRLEN; I ++) {(* T) [I] = S1 [I];} can be copied. (* T) [0] = MAXSTRLEN; return ERROR ;}// ----- print the specified string S ----- void StrOutput (SString S) {int I; printf ("Output string: "); for (I = 1; I <= S [0]; I ++) {printf (" % c", S [I]);} printf ("\ n");} // ----- intercepts the specified string, use Sub to return the string ----- Bool SubString (SString * Sub, SString S, int pos, int len) {int I, j; if (pos> = 1 & pos <= S [0]) & (len> 0 & len <= S [0]-pos + 1 )) {for (I = 1, j = pos; I <= len; I ++) {(* Sub) [I] = S [j ++];} (* Sub) [0] = len; return TURE;} return ERROR;} // ----- locate the first occurrence of string T from the pos position in string S, returns the new offset relative to S ----- int Index (SString S, SString T, int pos) {int m, n, I; SSt Ring sub; if (pos> 0) {n = StrLength (S); m = StrLength (T); I = pos; while (I <= n-m + 1) // Number of traversal times {SubString (& sub, S, I, m); if (0! = StrCompare (sub, T) {++ I;} else {return I ;}} return 0 ;} // ----- Replace all the strings T in string S with V ----- Bool Replace (SString * S, SString T, SString V) {int I = 1, j = 1, m = StrLength (T), n = StrLength (V); while (I <= (* S) [0]) // traverse all characters {j = Index (* S, T, I); // find the position of the matching string if (j> 0) // find the string {StrDelete (S, j, m); // Delete the string StrInsert (S, j, V ); // insert replacement string} I + = n + 1; // I ++; printf ("=======% d =======\ n ", i);} return TURE;} // ----- Insert the T string before the pos position of the string S. Note that the space of S should be large enough when inserting ----- Bool StrInsert (SString * S, int pos, SString T) {int m, n = StrLength (T), I = 1, j = pos; if (pos> = 1 & pos <= StrLength (* S) + 1 & (T [0] + (* S) [0] <= MAXSTRLEN )) {while (I <= n) {for (m = StrLength (* S) + 1; j <m; m --) {(* S) [m] = (* S) M-1]; // shifts one character behind the string to remove one space} (* S) [j] = T [I ++]; // copy the string T character to the string S (* S) [0] + = 1; // The length of the string is increased by 1 j + = 1; // move one after the insertion position} return TURE;} return ERROR ;} // ------ delete a substring whose length is len from the position of the pos in string S ------ Bool StrDelete (SString * S, int pos, int len) {int I = 1, j, m, n; if (pos> = 1 & pos <= StrLength (* S)-len + 1) {while (I <= len) {for (m = StrLength (* S), j = pos; j <= m; j ++) {(* S) [j] = (* S) [j + 1]; // forward a string} (* S) [0]-= 1; I ++;} return TURE;} return ERROR ;} // ----- destroy string ----- Bool DestroyString (SString * S) {free (S); return TURE ;}