Data structure (written by Yan Weimin in C language) - string

Regarding strings, the following only introduces the strings stored by heap allocation.

· Because the string of the heap-allocated storage structure has the characteristics of the sequential storage structure, it is easy to handle, and the length of the string is not limited during the operation.

  It has no restrictions and is more flexible, so it is often used in string processing applications.

More char arrays are similar

Paste the code directly below:

#include <stdio.h>
#include <stdlib.h>

typedef struct {
    char *ch;
    int length;
} HString;

int StrInsert(HString &S, int pos, HString T) {
    //Insert string T before the posth character of string S
    if (pos < 1 || pos > S.length + 1)
        return 0;
    if (T.length) {
        // allocate space
        if (!(S.ch = (char *) realloc((S.ch), (S.length + T.length) * sizeof(char))))
            exit(0);
        // To insert T, move the back of pos back to make room
        for (int i = S.length - 1; i < pos - 1; --i)
            S.ch[i + T.length] = S.ch[i];
        //insert
        for (int j = 0; j < T.length; ++j)
            S.ch[pos - 1 + j] = T.ch[j];
        S.length += T.length;
    }
    return 1;
}

int StrAssign(HString &T, char *chars) {
    //Generate a string T whose value is equal to the string constant chars
    int i;
    // release the original space
    if (T.ch)
        free(T.ch);
    for (i = 0; chars[i]; ++i);//Statistics chars length
    // empty string
    if (!i) {
        T.ch = NULL;
        T.length = 0;
    } else {
        // Copy one by one, which is equivalent to copying one copy
        if (!(T.ch = (char *) malloc(i * sizeof(char))))
            exit(0);
        T.length = i;
        for (; i > 0; i--)
            T.ch[i - 1] = chars[i - 1];
    }
    return 1;
}

int StrLength(HString S) {
    //return the number of elements of S, called the length of the string
    return S.length;
}

int StrCompare(HString S, HString T) {
    //S>T, the return value>0; if S=T, the return value=0;
    //If S<T, return value<0
    for (int i = 0; i < S.length && i < T.length; ++i) {
        if (S.ch[i] != T.ch[i])
            //Return the difference between the ASC codes of different characters at this time
            return S.ch[i] - T.ch[i];
    }
    return S.length - T.length;
}

int ClearString(HString &S) {
    // clear S to an empty string
    if (S.ch) {
        free(S.ch);
        S.ch = NULL;
    }
    S.length = 0;
    return 1;
}

int Concat(HString &T, HString S1, HString S2) {
    //Use T to return a new string formed by connecting S1 and S2
    // release old space
    if (T.ch)
        free(T.ch);
    // allocate space
    if (!(T.ch = (char *) malloc((S1.length + S2.length) * sizeof(char))))
        exit(0);
    //put into S1
    for (int i = 0; i < S1.length; ++i) {
        T.ch[i] = S1.ch[i];
    }
    // total length
    T.length = S1.length + S2.length;
    //put into S2
    for (int j = S1.length; j < T.length; ++j) {
        T.ch[j] = S2.ch[j - S1.length];
    }
    return 1;
}

int SubString(HString &Sub, HString S, int pos, int len) {
    //Use Sub to return a string of length len from the posth character of the string S
    //其中,1<=pos<=StrLength(S),且0<=len<=StrLength(S)-pos+1
    if (pos < 1 || pos > S.length || len < 0 || len > S.length - pos + 1)
        return 0;
    // release old space
    if (Sub.ch)
        free(Sub.ch);
    // empty substring
    if (!len) {
        Sub.ch = NULL;
        Sub.length = 0;
    } else {
        // allocate space
        Sub.ch = (char *) malloc(len * sizeof(char));
        // copy substring
        for (int i = 0; i < len; ++i) {
            Sub.ch[i] = S.ch[pos - 1 + i];
        }
        Sub.length = len;
    }
    return 1;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325807206&siteId=291194637