#include <stdio.h>#include<tchar.h>#include<stdlib.h>//TODO: Refer to other header files required by the program herestructstring{Char*ch; intlength;};BOOLAssign_string (string* str,Char*chars);BOOLDestroy_string (string*str);BOOLClear_string (string*str);voidprint_string (String str);BOOLInsert_string (string* str, String T,intlocate);BOOLCopy_string (string*str, String T);intindex_string (String str, string T);//re-assigning a string to a valueBOOLAssign_string (string* str,Char*chars) { if(STR->CH) Free(str->ch); inti; Char*C; for(i =0, C = chars; *c; C + +, i++);//Get string length if(!i) {str->ch = NULL; str->length =0;return true; } Else{str->ch = (Char*)malloc(i*sizeof(Char)); for(intj =0; J < I; J + +) {str->CH[J] =Chars[j]; } STR->length =i; return true; }}//destroying a stringBOOLDestroy_string (string*str) { if(str) { Free(str); STR=NULL; return true; } Else return false;}//empty stringBOOLClear_string (string*str) { while(str->length) {STR->ch[str->length] =NULL; STR->length--; } return true;}//Printvoidprint_string (String str) {if(str.length) { for(inti =0; I < str.length;i++) {printf ("%c", Str.ch[i]); } printf ("\ n"); }}//inserts a substring t before the first locate element of the original string strBOOLInsert_string (string* str, String T,intLocate) { if(locate<0|| Locate>str->length)return false; if(t.length) {if(! (Str->ch = (Char*)realloc(Str->ch, (str->length + t.length) *sizeof(Char))) Exit (-1); for(inti = str->length-1; I >= locate; i--) Str->ch[i + t.length] = str->Ch[i]; for(inti =0; i < t.length; i++) Str->ch[locate + i] =T.ch[i]; STR->length + =t.length; } return true;}//assign the value of T to strBOOLCopy_string (string*str, String T) { if(STR->CH) Free(str->ch); if(!&t) {str->ch = NULL; str->length =0;return true; } Else{str->ch = (Char*)malloc(t.length*sizeof(Char)); for(inti =0; i < t.length; i++) {str->ch[i] =T.ch[i]; } STR->length =t.length; } return true;}//search for the position of the substring T in the original string str//Lookup failed return-1//Normal return index is the first occurrence of the T-first character positionintindex_string (String str, string T) {intindex =-1; intKey =0; intHl =0; if(t.length&&str.length>=t.length) { for(inti =0; I <= str.length-t.length; i++){ if(Str.ch[i] = =T.ch[key]) {Key++; Hl++; if(Hl >= t.length) {printf ("It's coming out \ n"); index = i-t.length+1;returnindex;} } Else{Hl= key =0; } printf ("I=%d,str is%c,t is%c, checking the%d character of T, which has met the number of characters%d\n", I, Str.ch[i], T.ch[key], key, Hl); } } returnindex;}
C language implements the basic data structure (vi) string