고정 순서 문자열 기본 조작 실현

고정 순서 문자열 기본 조작 실현
//
// 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("
"
); 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("
"
); printf(" str = a,b,c,d,e,f s
"
); StrAssign(&s, str, length); printf(" s:"); Out(&s); printf(" s m
"
); StrCopy(&s, &m); printf(" m:"); Out(&m); printf(" s m
"
); 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
"
); 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
"
, states); printf(" m s 1
"
); InsertString(&s, m, 1); printf(" s:"); Out(&s); printf(" s 6
"
); DelString(&s, 1, 6); printf(" s:"); Out(&s); }

좋은 웹페이지 즐겨찾기