数据结构——C语言实现String字符串操作

#include 
#include 
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

#define Status int
typedef char * string;

//返回串s的元素个数,即串的长度
int StrLength(string s){
    if(s != NULL){
        int i = 0;
        while(s[i] != '\0'){
            i++;
        }
        return i;
    }
    return 0;
}

//生成一个其值等于字符串常量s的串t
Status StrAssign(string *t,string s){
    int len = StrLength(s);
    if(len > 0){
        *t = (char *)malloc(sizeof(char)*len);
        int i = 0;
        while(i < len){
            (*t)[i] = s[i];
            i++;
        }
        (*t)[i] = '\0';
    }
    return ERROR;
}

//串s存在,由串s复制得串t
Status StrCopy(string *t, string s){
    *t = s;
    return OK;
}

//串s存在,将串清空
Status ClearString(string *s){
    if(s){
        free(*s);//释放后变成野指针,必须置为空指针
        *s = NULL;
    }
    return OK;
}

//若串s为空,返回true,否则返回false
Status StringEmpty(string s){
    if(s){
        return FALSE;
    }
    return TRUE;
}

//若s>t,返回值>0,若s=t,返回0,若s lent){
        return i - lent - 1;
    }else{
        return -1;
    }
}

//串s,t和v存在,t是非空串。用v替换主串s中出现的所有与t相等的不重叠的子串
string Replace(string s,string t,string v){
    int i = 0;
    int j = 0;
    int k = 0;
    int index = Index(s,t,0);
    if(index < 0){
        return ERROR;
    }
    int lens = StrLength(s);
    int lent = StrLength(t);
    int lenv = StrLength(v);
    char *newS = (char *)malloc(sizeof(char) * (lens + lenv - lent));
    while(i < index){
        newS[k] = s[i];
        i++;
        k++;
    }
    while(j < lenv){
        newS[k] = v[j];
        j++;
        k++;
    }
    for(i = index + lent; i < lens; i++){
        newS[k] = s[i];
        k++;
    }

    return newS;
}

//串s,t存在,1<=pos<=strlength(s) + 1,在串s的第pos个字符之前插入串t
string StrInsert(string s,int pos,string t){
    int lens = StrLength(s);
    int lent = StrLength(t);
    int i = 0;
    int j = 0;
    int k = 0;
    int index = pos - 1;
    string res = (char *)malloc(sizeof(char) * (lens + lent));
    while(k < index){
        res[i] = s[k];
        i++;
        k++;
    }
    while(j < lent){
        res[i] = t[j];
        j++;
        i++;
    }
    for(k = index;k < lens;k++){
        res[i] = s[k];
        i++;
    }
    res[i] = '\0';
    return res;
}

//串s存在,1<=pos<=strlength(s) - len + 1.从串s中删除第pos个字符起长度为len的子串
string StrDelete(string s,int pos, int len){
    int lent = StrLength(s);
    if(len > lent - pos || len <= 0){
        return NULL;
    }
    string res = (char *)malloc(sizeof(char) * (lent - len));
    int i = 0;
    int k = 0;
    while(i < pos){
        res[k] = s[i];
        i++;
        k++;
    }
    for(i = pos + len ; i < lent; i++){
        res[k] = s[i];
        k++;
    }
    res[k] = '\0';
    return res;
}

int main(){
    string t = NULL;

    string s = "I like DataStructure very much!!!";//字符串常量
    StrAssign(&t,"I like DataStructure very much!!!");
    printf("t = %s\n",t);
    t[0] = 'a';
    printf("t = %s\n",t);

    printf("len = %d\n",StrLength(s));

    ClearString(&t);
    printf("t = %s\n",t);

    StrCopy(&t,s);
    printf("t = %s\n",t);

    printf("result = %d\n",StringEmpty(t));
    t = NULL;
    printf("result = %d\n",StringEmpty(t));

    t = "hello";
    s = ",world.";
    printf("s - t = %d\n",StrCompare(s,t));

    string s_new;
    StrConcat(&s_new,t,s);
    printf("_msize = %d\n",_msize(s_new));
    printf("%s\n",s_new);


    string sub;
    string ss = "hello,world.";
    SubString(&sub,ss,3,110);
    printf("sub = %s\n",sub);

    string t_new = "rl.";
    int i = Index(ss,t_new,0);
    printf("i = %d\n",i);

    string sss= "www.hello.world";
    string ttt = "hello";
    string newsss = Replace(sss,ttt,"aa");
    printf("newsss = %s\n",newsss);

    string res = StrInsert(sss,2,ttt);
    printf("res = %s \n",res);
    
    printf("res = %s\n",StrDelete(sss,3,2));
    return 0;
}

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