c語言實現基本的資料結構(六) 串

來源:互聯網
上載者:User

標籤:列印   struct   lib   實現   ssi   index   元素   出現   real   

#include <stdio.h>#include <tchar.h>#include <stdlib.h>// TODO:  在此處引用程式需要的其他標頭檔struct String{    char* ch;    int length;};bool Assign_String(String* str, char* chars);bool Destroy_String(String* str);bool Clear_String(String* str);void Print_String(String str);bool Insert_String(String* str, String T, int locate);bool Copy_String(String* str, String T);int Index_String(String str, String T);//給字串重新賦值bool Assign_String(String* str,char* chars){    if (str->ch) free(str->ch);    int i;    char* c;    for (i = 0, c = chars; *c; c++, i++);//擷取字串長度    if (!i) { str->ch = NULL; str->length = 0; return true; }    else{        str->ch = (char*)malloc(i*sizeof(char));        for (int j = 0; j < i; j++){            str->ch[j] = chars[j];        }        str->length = i;        return true;    }}//銷毀字串bool Destroy_String(String* str){    if (str)    {        free(str);        str = NULL;        return true;    }    else        return false;}//清Null 字元串bool Clear_String(String* str){    while (str->length){        str->ch[str->length] = NULL;        str->length--;    }    return true;}//列印void Print_String(String str){    if (str.length)    {        for (int i = 0; i < str.length;i++)        {            printf("%c", str.ch[i]);                    }        printf("\n");    }}//在原字串str的第locate個元素前插入子字串Tbool Insert_String(String* str, String T, int locate){    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 (int i = str->length - 1; i >= locate; i--)            str->ch[i + T.length] = str->ch[i];        for (int i = 0; i < T.length; i++)            str->ch[locate + i] = T.ch[i];        str->length += T.length;    }    return true;}//把T的值賦給strbool Copy_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 (int i = 0; i < T.length; i++){            str->ch[i] = T.ch[i];        }        str->length = T.length;    }    return true;}//在原字串str中搜尋子字串T的位置//尋找失敗返回-1//正常返回index為第一次出現T首字元的位置int Index_String(String str, String T){    int index = -1;    int key = 0;    int Hl = 0;    if (T.length&&str.length>=T.length){        for (int i = 0; i <= str.length - T.length; i++){            if (str.ch[i] == T.ch[key]){                key++;                Hl++;                if (Hl >= T.length) { printf("出來了\n"); index = i - T.length+1; return index; }            }            else{                Hl = key = 0;            }            printf("i=%d,str是%c,T是%c,核對T的第%d個字元,已滿足字元數%d\n", i, str.ch[i], T.ch[key], key, Hl);        }    }    return index;}

 

c語言實現基本的資料結構(六) 串

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.