String programming, replace the sub-string T1 that appears in string s with a string T2.

xiaoxiao2021-03-06  112

/ * String programming, substitution of sub-string T1 appearing in string s with strings T2

Ahebhechedhe

HE

Hello!

AHELLO! BHELLO! Chello! Dhello!

* // * Static array implementation * / # include #include #include

#define maxStrlen 255

Typedef unsigned char sstring [MaxStrlen 1]; TypeDef Int Status;

Void Strassign (SSTRING & S, CHAR * Chars) {// String value INT length = 0; unsigned char * SCLIENT = S 1;

While (* (chars)) {* SCLIENT = * Chars; Chars ; SCLIENT ; Length ;} * SCLIENT = '/ 0'; s [0] = Length;}

Void Display_String (SSTRING S) {// String Displays Cout << S 1 << endl;}

INT INDEX (SSTRING S, SSTRING T, INT POS) {// Scan the location value of the sub-string T in the string S, such as not there is a substring t to return 0 UNSIGNED CHAR * SCLIENT = S 1; int clientlen = 0;

IF (POS> S [0]) RETURN-1; T ; while (* (splient)) {while (* (t) == * (SCLIENT POS)) {t ; if (! * t) return POS; Clientlen ; SCLIENT ;} SCLIENT = SCLIENT - Clientlen; POS ;} return -1;} void delete (SSTRING & S, INT POS, INT LEN) {// Delete the LEN character starting from the POS position in string S [ 0] - = le; unsigned char * sclient = s 1;

While (* (SCLIENT LEN POS)) {* (SCLIENT POS) = * (SCLIENT POS LEN); SCLIENT ;} * (SCLIENT POS) = '/ 0';}

Void Insert (SSTRING & S, INT & POS, SSTRING T) {// Insert subtrings T INT I in the POS position of Skew S;

IF (POS! = S [0]) {for (i = 0; i

Void replace_substring (SSTRING & S, SSTRING T1, SSTRING T2) {// Replace INT POS = 0 by the string T1 that appears in the string S, by the call to the INDEX, DELETE, and INSERT functions, to replace INT POS = 0; int POSFLAG = -1 While (1) {POS = INDEX (S, T1, POS); IF (POS

STRASSIGN (S, "Ahebhechedhe"); Display_String (s); Strassign (T1, "HE"); Display_String (T1); Strassign (T2, "Hello!"); Display_String (T2) ;. DISPLAY_STRING (T2);

Replace_substring (s, t1, t2); display_string (s);} / * Implement with dynamic linies: * / # include #include

#define maxStrlen 255

TypedEf struct {char * ch; int layth;} hstring

Void Strassign (HSTRING & S, Char * Chars) {// Strous value Char * C; INT I, J;

IF (! s .ch) free (s.ch);

For (i = 0, c = chars; * c; c , i );

IF (! i) {s.ch = null; s.Length = 0;} else {if (! (S.CH = (char *)))) Return; for J = 0; j

Void Display_String (HString S) {// String Displays if (S.CH == NULL) Return; INT i; for (i = 0; I

INT INDEX (HString S, HString T, INT POS) {// Scan the position value of the sub-string T in the string S, if there is no sub-string T returns 0 int clientlen = 0; char * tclient = T.CH; IF POS> = S.LENGTH) RETURN-1; char * SCLIENT = S.CH; while ((SCLIENT - S.CH) <= s.Length) {while (* (tclient) == * (SCLIENT POS)) {IF (Tclient - T.CH)

Void Insert (HString & S, INT & POS, HSTRING T) {// In the POS position in the string S.Ch = (char *) Realloc (s.ch, t.length s.length; s, Length = T.LENGTH; INT I; IF (POS! = S.LENGTH) {for (i = 0; i

Void replace_substring (HSTRING & S, HSTRING T1, HSTRING T2) {// Replace INT POS = 0 by the string T1 that appears in the string S in the bundle T1 of the index, delete, and INSERT functions; int POSFLAG = -1 While (1) {POS = INDEX (S, T1, POS); IF (POS

Void main () {HString S, T1, T2;

STRASSIGN (S, "Ahebhechedhe"); Display_String (s); Strassign (T1, "HE"); Display_String (T1); Strassign (T2, "Hello!"); Display_String (T2); Replace_Substring (s, t1, t2 ); Display_string (s);

转载请注明原文地址:https://www.9cbs.com/read-102982.html

New Post(0)