【23王道数据结构】串的基本操作

//
// Created by 48272 on 2022/4/20.
//
#include 

using namespace std;
const int MAXSIZE = 20;

typedef struct {
    char data[MAXSIZE];
    int length;
}String;

void Init(String &s);
void StrCopy(String &T, String s);
bool StrEmpty(String s);
int StrLength(String s);
void ClearString(String &s);
void DestroyString(String &s);
bool Concat(String &t, String s1, String s2);
bool SubString(String &t, String s, int pos, int length);
int Index(String s, String t);
bool StrCompare(String s, String t);
void print(String s);

int main(void) {
    String s,t,st;
    Init(s);
    s.data[1] = 'w';
    s.data[2] = 'o';
    s.data[3] = 'a';
    s.data[4] = 'i';
    s.data[5] = 'n';
    s.data[6] = 'i';
    s.length = 6;

    printf("==================================================\n");
    StrCopy(t,s);
    printf("执行StrCopy后:");
    print(t);
    printf("==================================================\n");
    printf("t是否为空:%d\n", StrEmpty(t));

    printf("==================================================\n");
    Concat(st,s,t);
    printf("执行过Concat后 st:");
    print(st);

    printf("==================================================\n");
    SubString(t,s,4,2);
    printf("执行完Substring后:");
    print(t);

    printf("==================================================\n");
    printf("比较t && s %d\n", StrCompare(t,s));

    int index = Index(s,t);
    if (index == -1) printf("未找到!\n");
    else printf("t在s中的位置是%d\n",index);

}

void Init(String &s) {
    s.length = 0;
}
void StrCopy(String &T, String s) {
    for (int i = 1; i <= s.length; i++) T.data[i] = s.data[i];
    T.length = s.length;
}
bool StrEmpty(String s) {
    return s.length < 1;
}
int StrLength(String s) {
    return s.length;
}
void ClearString(String &s) {
    s.length = 0;
}
void DestroyString(String &s){
    for (int i = 1; i <= s.length; i++) s.data[i] = '\0';
    s.length = 0;
}

bool Concat(String &t, String s1, String s2) {
    t.length = s1.length + s2.length;
    int i = 1;
    for (; i <= s1.length; i++) t.data[i] = s1.data[i];
    for (; i <= t.length; i++) t.data[i] = s2.data[i - s1.length];
    return true;
}
bool SubString(String &t, String s, int pos, int length) {
    if (pos + length - 1 > s.length) return false;

    for (int i = pos; i < pos + length; i++) t.data[i - pos + 1] = s.data[i];
    t.length = length;
    return true;
}
bool StrCompare(String s, String t) {

    for (int i = 1; i <= s.length; i++) {
        if (s.data[i] != t.data[i]) return s.data[i] - t.data[i];
    }
    //此时遍历完S,我们不知道T是否还有剩余元素
    return s.length - t.length;
}
//返回 t在s中的位置
int Index(String s, String t) {
    int length1 = StrLength(s);
    int length2 = StrLength(t);
    if (length2 > length1) return -1;

    int i = 1;
    String sub;
    while (i <= (length1 - length2 +  1)) {
        SubString(sub,s,i,length2);
        print(sub);

        if (StrCompare(sub, t) == 0) return i;

        i++;
    }
    return -1;
}

void print(String s) {
    for (int i = 1; i <= s.length; i++) printf("%c",s.data[i]);
    printf("\n");
}

你可能感兴趣的:(数据结构与算法,数据结构)