C language implements the basic data structure (vi) string

Source: Internet
Author: User

#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-&GT;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-&GT;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-&GT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.