Data Structure-sequential string storage in C Language
// Indicates the sequential storage of C strings // indicates the heap distribution storage of strings // Yang Xin # include
# Include
# Include
# Define MAXSTRLEN 255 # define TRUE 1 # define FALSE 0 # define OK 1 # define ERROR 0 # define INFEASIBLE-1 # define OVERFLOW-2 // define the typedef int status; typedef struct {char * ch; int length;} HString; // generate a string whose value is equal to the String constant chars T Status StrAssign (HString * T, char * chars) {int I, j; if (* T ). ch) free (* T ). ch); I = strlen (chars); if (! I) {(* T ). ch = NULL; (* T ). length = 0;} else {(* T ). ch = (char *) malloc (I * sizeof (char); if (! (* T). ch) exit (OVERFLOW); for (j = 0; j
S. length | len <0 | len> S. length-pos + 1) return ERROR; if (* Sub ). ch) free (* Sub ). ch); if (! Len) {(* Sub ). ch = NULL; (* Sub ). length = 0;} else {(* Sub ). ch = (char *) malloc (len * sizeof (char); if (! (* Sub ). ch) exit (OVERFLOW); for (I = 0; I <= len-1; I ++) (* Sub ). ch [I] = S. ch[ pos-1 + I]; (* Sub ). length = len;} return OK;} void InitString (HString * T) {(* T ). length = 0; (* T ). ch = NULL;} int Index (HString S, HString T, int pos) {int n, m, I; HString sub; InitString (& sub); 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;} Status StrInsert (HString * S, int pos, HString T) {int I; if (pos <1 | pos> (* S ). length + 1) return ERROR; if (T. length) {(* S ). ch = (char *) realloc (* S ). ch, (* S ). length + T. length) * sizeof (char); if (! (* S ). ch) exit (OVERFLOW); for (I = (* S ). length-1; I >= pos-1; -- I) (* S ). ch [I + T. length] = (* S ). ch [I]; for (I = 0; I
'; Printf ("string s % c string t \ n", c); Concat (& r, t, s ); printf ("the r produced by string t join string s is:"); StrPrint (r); StrAssign (& s, "oo"); printf ("the string s is: "); StrPrint (s); StrAssign (& t," o "); printf (" string t: "); StrPrint (t); Replace (& r, t, s); printf ("after replacing the substring in string r with the substring in string t with string s, the substring r is: \ n"); StrPrint (r ); clearString (& s); printf ("after string s is cleared, is the string length: % d empty? % D (1: NULL 0: No) \ n ", StrLength (s), StrEmpty (s); SubString (& s, r, 6, 4 ); printf ("string s is four characters starting from the string r's 6th characters, Length: % d string s:", s. length); StrPrint (s); StrCopy (& t, r); printf ("Copy string t is string r, string t is:"); StrPrint (t ); strInsert (& t, 6, s); printf ("after the string s is inserted before the string t's 6th characters, the string t is:"); StrPrint (t ); strDelete (& t, 1st); printf ("the string t is:"); StrPrint (t) after 5 characters are deleted from the string t ); printf ("% d is the position of the 1st substring \ n", Index (t, s, 1) that is the same as the string s, starting from the 1st characters of the string t )); printf ("% d is the position of the 2nd substring \ n", Index (t, s, 2) that is the same as the string s, starting from the 1st characters of the string t )); return 0 ;}