定长顺序串基本操作实现

定长顺序串基本操作实现

//
// Created by Changepeng on 2019/6/18.
//

#include
#include

#define maxsize 100
typedef int states;
typedef char datatype;
#define ERROR 0
#define FALSE 0
#define OK 1
#define TRUE 1


typedef struct {
     
    char ch[maxsize];
    int length;
} String, *string;

//初始化
states initString(string s) {
     
    s->length = 0;
    return OK;
}

//串赋值
states StrAssign(string s, char str[], int length) {
     
    int i;
    for (i = 0; i < length; i++) {
     
        s->ch[i] = str[i];
    }
    s->length = length;
    return OK;
}

//串复制
states StrCopy(string s, string str) {
     
    int i;
    for (i = 0; i < s->length; i++) {
     
        str->ch[i] = s->ch[i];
    }
    str->length = s->length;
    return OK;
}

//判断长度超出
states StringFull(string s) {
     
    if (s->length > maxsize) {
     
        return TRUE;
    } else {
     
        return FALSE;
    }
}

//判空串
states NullString(string s) {
     
    if (s->length == 0) {
     
        return TRUE;
    } else {
     
        return FALSE;
    }
}

//串连接
states Concat(string s, String str) {
     
    int length;
    int i;
    length = s->length + str.length;
    if (length <= maxsize) {
     
        for (i = 0; i < str.length; i++) {
     
            s->ch[s->length + i] = str.ch[i];
//            printf("%c",str.ch[i]);
        }
        s->length = length;
        return OK;
    } else {
     
        return ERROR;
    }
}

//求子串函数
states SubStr(string s, int i, int j, string str) {
     
    int k;
    if (NullString(s) == TRUE) {
     
        return ERROR;
    }
    for (k = i - 1; k < j; k++) {
     
        str->ch[k - i + 1] = s->ch[k];
    }
    str->length = j - i + 1;
    return OK;
}

//串替换函数
states ReqString(string s, String str, int i) {
     
    int j;
    for (j = i - 1; j < i + str.length; j++) {
     
        s->ch[j] = str.ch[j - i + 1];
//        printf("%c",str.ch[j - i + 1]);
    }
    return OK;
}

//串比较函数
states CompareString(String s, String x) {
     
    int i;
    int k = 0;
    if (s.length != x.length) {
     
        return FALSE;
    }
    for (i = 0; i < s.length; i++) {
     
        if (s.ch[i] == x.ch[i]) {
     
            k++;
        }
    }
    if (k == s.length) {
     
        return TRUE;
    } else {
     
        return FALSE;
    }
}

//串插入
states InsertString(string s, String str, int i) {
     
    int j, k;
    k = s->length;
    int length = s->length + str.length;
    if (length > maxsize) {
     
        return ERROR;
    }
    s->length = length;
    for (j = length - 1; j >= k - str.length; j--) {
     
        s->ch[j] = s->ch[j - str.length];
    }
    for (j = 0; j < str.length; j++) {
     
        s->ch[j + i - 1] = str.ch[j];
    }
    return OK;
}

//串删除
states DelString(string s, int i, int j) {
     
    int k;
    if ((j - i) > s->length) {
     
        return ERROR;
    }
    for(k = i-1;k<(s->length-j);k++){
     
        s->ch[k] = s->ch[k+j-i+1];
    }
    s->length -= (j-i+1);

}

//遍历串
states Out(string s) {
     
    int i;
    for (i = 0; i < s->length; i++) {
     
        printf("%c", s->ch[i]);
    }
    printf("\n");
    return OK;
}

int main() {
     
    String s, m, n;
    int length, states;
    char str[6] = {
     'a', 'b', 'c', 'd', 'e', 'f'};
    char change[6] = {
     'g', 'h', 'i', 'j', 'k', 'l'};
    length = sizeof(str) / sizeof(str[0]);
    initString(&s);
    printf("初始化完成\n");
    printf("将str = a,b,c,d,e,f赋值给s\n");
    StrAssign(&s, str, length);
    printf("遍历输出s:");
    Out(&s);
    printf("将s复制给m\n");
    StrCopy(&s, &m);
    printf("遍历输出m:");
    Out(&m);
    printf("将s和m连接\n");
    Concat(&s, m);
    printf("遍历输出s:");
    Out(&s);
    printf("求s中的第1个到第5个字符给n");
    SubStr(&s, 1, 5, &n);
    printf("遍历输出n:");
    Out(&n);
    printf("将s中的后面的abcdef替换成ghijkl\n");
    length = sizeof(change) / sizeof(change[0]);
    StrAssign(&m, change, length);
    ReqString(&s, m, 7);
    printf("遍历输出s:");
    Out(&s);
    length = sizeof(change) / sizeof(change[0]);
    StrAssign(&n, change, length);
    states = CompareString(m, n);
    printf("将ghijkl赋值给n,判断此时m和n是否相同(相同为1,不相同为0):");
    printf("%d\n", states);
    printf("将m插入到s的位置1之前\n");
    InsertString(&s, m, 1);
    printf("遍历输出s:");
    Out(&s);
    printf("删除s前6位\n");
    DelString(&s, 1, 6);
    printf("遍历输出s:");
    Out(&s);
}

你可能感兴趣的:(串,c语言,数据结构)