Fourth programming exercise: string programming, replacing the substrings T1 appearing in the string S with a string T2.

xiaoxiao2021-03-06  17

Description:

1 Allocate the storage structure to store strings with a heap.

2 Type definitions and main functions cannot be modified, and make up the required functions.

3 function of the function of the function is described in the text provided

Run results: Ahebhechedhehehehello! Ahello! Bhello! Chello! Dhello!

#include #include #include // # Define MaxStrlen 255 # Define Ok 1 # Define Error 0 # define overflow 0typedef int stat;

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

Status Strassign (HString & S, Char * Chars) {INT I, J; Char * C; if (S.CH) Free (S.CH); for (i = 0, c = chars; * C; i , C ) ; If (! I) {s.ch = null; s.Length = 0;} else {if (! (S.ch = (char *) Malloc (i * sizeof (char)))) exit (overflow); For (j = 0; j

Void Display_String (HString S) {INT i; for (i = 0; i

INDEX (HString S, HString T, INT POS) {INT I, J; I = POS-1; J = 0; While (i

Void Delete (HString & S, INT POS, INT LEN) {HString L; INT I; IF (! (l.ch = (char *) Malloc (S.Length-Len) * Sizeof (char))) EXIT ( 0); for (i = 0; i

Void INSERT (HString & S, INT POS, HSTRING T) {HString L; INT I; IF (! (l.ch = (char *) Malloc ((S.Length L.Length) * Sizeof (char))))))) Exit (0); for (i = 0; i

Void main () {HSTRING S, T1, T2; Strassign (s, "Ahebhechedhe"); Display_String (s); coupt << endl; strassign (t1, "he"); display_string (t1); cout << end1 STRASSIGN (T2, "Hello!"); Display_String (T2); cout << Endl; Replace_substring (S, T1, T2); Display_String (s);}

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

New Post(0)