堆串的应用

比如串S1=“Welcome to”,S2="China",Sub="Xi'an",将串S2连接到串S1末尾,然后将串S1中的S2用Sub替换。
#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
  
typedef struct  
{  
    char *str;  
    int length;  
}HeapString;  
  
void InitString(HeapString *S);//串的初始化操作  
void StrAssign(HeapString *S,char cstr[]);//串的赋值操作  
int StrEmpty(HeapString S);//推断串是否为空  
int StrLength(HeapString S);//求串的长度操作  
void StrCopy(HeapString *T,HeapString S);//串的复制操作  
int StrCompare(HeapString S,HeapString T);//串的比較操作  
int StrInsert(HeapString *S,int pos,HeapString T);//串的插入操作  
int StrDelete(HeapString *S,int pos,int len);//串的删除操作  
int StrConcat(HeapString *T,HeapString S);//串的连接操作  
int SubString(HeapString *Sub,HeapString S,int poos,int len);//截取子串操作  
int StrReplace(HeapString *S,HeapString T,HeapString V);//串的替换操作  
int StrIndex(HeapString S,int pos,HeapString T);//串的定位操作  
void StrClear(HeapString *S);//清空串操作  
void StrDestory(HeapString *S);//摧毁串操作  
void StrPrint(HeapString S);//串的输出声明 


#include "HeapString.h"  
  
void InitString(HeapString *S)  
{  
    S->length = 0;  
    S->str = '';  
}  
void StrAssign(HeapString *S,char cstr[])//串的赋值操作(将常量cstr中的字符赋值给串S)  
{  
    int i = 0,len;  
    if(S->str)  
    {  
        free(S->str);  
    }  
    for(i = 0;cstr[i]!='';i++)  
    {  
        ;  
    }  
    len = i;  
    if(!i)  
    {  
        S->length = 0;  
        S->str = '';  
    }  
    else  
    {  
        S->str = (char*)malloc(len*sizeof(char));  
        if(!S->str)  
        {  
            exit(-1);  
        }  
        for(i = 0;i < len;i++)  
        {  
            S->str[i] = cstr[i];  
        }  
        S->length = len;  
    }  
}  
int StrEmpty(HeapString S)//推断串是否为空  
{  
    if(S.length == 0)  
    {  
        return 1;  
    }  
    else  
    {  
        return 0;  
    }  
}  
int StrLength(HeapString S)//求串的长度操作  
{  
    return S.length ;  
}  
void StrCopy(HeapString *T,HeapString S)//串的复制操作(将串S中的每个字符赋给T)  
{  
    int i;  
    T->str = (char*)malloc(S.length*sizeof(char));  
    if(!T->str)  
    {  
        exit(-1);  
    }  
    for(i = 0;i < S.length ;i++)  
    {  
        T->str[i] = S.str[i];  
    }  
    T->length = S.length ;  
}  
int StrCompare(HeapString S,HeapString T)//串的比較操作  
{  
    int i;  
    for(i = 0;i < S.length&&i < T.length ;i++)//比較两个串中的字符  
    {  
        if(S.str[i] != T.str[i])//假设出现字符不同。则返回两个字符的差值  
        {  
            return (S.str[i]-T.str[i]);  
        }  
    }  
    return (S.length - T.length);//假设比較完成,返回两个字符串的长度的差值  
}  
int StrInsert(HeapString *S,int pos,HeapString T)//串的插入操作(在串S的pos个位置插入串T)  
{  
    int i;  
    if(pos < 0 || pos-1 > S->length)  
    {  
        printf("插入位置不对
");  
        return 0;  
    }  
    S->str = (char*)realloc(S->str,(S->length+T.length)*sizeof(char));  
    if(!S->str)  
    {  
        printf("内存分配失败");  
        exit(-1);  
    }  
    for(i = S->length -1;i >= pos-1;i--)  
    {  
        S->str[i+T.length] = S->str[i];  
    }  
    for(i = 0;i < T.length ;i++)  
    {  
        S->str[i+pos-1] = T.str[i];  
    }  
    S->length = S->length + T.length;  
    return 1;  
}  
int StrDelete(HeapString *S,int pos,int len)//串的删除操作(在串S中删除pos開始的len个字符,然后将后面的字符向前移动)  
{  
    int i;  
    char *p;  
    if(pos < 0 || len < 0 || pos+len-1 > S->length)  
    {  
        printf("删除位置不对,參数len不合法
");  
        return 0;  
    }  
    p = (char*)malloc(S->length-len);  
    if(!p)  
    {  
        exit(-1);  
    }  
    for(i = 0;i < pos-1;i++)//将串第pos位置之前的字符拷贝到p中  
    {  
        p[i] = S->str[i];  
    }  
    for(i = pos-1;i < S->length-len;i++)//将串第pos+len位置以后的字符拷贝到p中  
    {  
        p[i] = S->str[i+len];  
    }  
    S->length = S->length -len;//改动串的长度  
    free(S->str);//释放原来的串S的内存空间  
    S->str = p;//将串的str指向p字符串  
    return 1;  
}  
int StrConcat(HeapString *T,HeapString S)//串的连接操作(将串S连接在串T的后面)  
{  
    int i;  
    T->str = (char*)realloc(T->str ,(T->length +S.length )*sizeof(char));  
    if(!T->str)  
    {  
        printf("分配空间失败");  
        exit(-1);  
    }  
    else  
    {  
        for(i = T->length ;i < T->length +S.length ;i++)//将串S直接连接到T的末尾  
        {  
            T->str[i] = S.str[i-T->length];  
        }  
        T->length = T->length +S.length ;//改动串T的长度  
    }  
    return 1;  
}  
int SubString(HeapString *Sub,HeapString S,int pos,int len)//截取子串操作(截取串S中从第pos个字符開始,长度为len的连续字符。并赋值给Sub)  
{  
    int i;  
    if(Sub->str)  
    {  
        free(Sub->str);  
    }  
    if(pos < 0 || len < 0 || pos+len-1 > S.length)  
    {  
        printf("參数pos和len不合法
");  
        return 0;  
    }  
    else  
    {  
        Sub->str = (char*)malloc(len*sizeof(char));  
        if(!Sub->str)  
        {  
            printf("存储分配失败");  
            exit(-1);  
        }  
        for(i = 0;i < len;i++)  
        {  
            Sub->str[i] = S.str[i+pos-1];  
        }  
        Sub->length = len;  
        return 1;  
    }  
}  
int StrIndex(HeapString S,int pos,HeapString T)//串的定位操作(在主串S中的第pos个位置開始查找子串T,假设主串S中存在与串T值相等的子串,返回子串在主串第pos个字符后第一次出现的位置)  
{  
    int i,j;  
    if(StrEmpty(T))  
    {  
        return 0;  
    }  
    i = pos;  
    j = 0;  
    while(i < S.length && j < T.length)  
    {  
        if(S.str[i] == T.str[j])  
        {  
            i++;  
            j++;  
        }  
        else//假设当前相应位置的字符不相等。则从串S的下一个字符開始,从T的第0个字符開始比較  
        {  
            i = i-j+1;  
            j = 0;  
        }  
    }  
    if(j >= T.length)//假设在串S中找到串T,则返回子串T在主串S中的位置  
    {  
        return i-j+1;  
    }  
    else  
    {  
        return -1;  
    }  
}  
int StrReplace(HeapString *S,HeapString T,HeapString V)//串的替换操作(假设串S中存在子串T,则用V替换串S中的全部子串T)  
{  
    int flag;  
    int i = 0;  
    if(StrEmpty(T))  
    {  
        return 0;  
    }  
    do  
    {  
        i = StrIndex(*S,i,T);//利用串的定位操作在串S中查找T的位置  
        if(i)  
        {  
            StrDelete(S,i,StrLength(T));//假设找到子串T,则将S中的串T删除  
            flag = StrInsert(S,i,V);//将V插入  
            if(!flag)  
            {  
                return 0;  
            }  
            i += StrLength(V);  
        }  
    }while(i);  
    return 1;  
}  
void StrClear(HeapString *S)//清空串操作  
{  
    if(S->str)  
    {  
        free(S->str);  
    }  
    S->str = '';  
    S->length = 0;  
}  
void StrDestory(HeapString *S)//摧毁串操作  
{  
    if(S->str)  
    {  
        free(S->str);  
    }  
}  
void StrPrint(HeapString S)//串的输出声明  
{  
    int i;  
    for(i = 0;i < S.length ;i++)  
    {  
        printf("%c",S.str[i]);  
    }  
    printf("
");  
}  



#include "HeapString.h"  

int main(void)  
{  
    HeapString S1,S2,Sub;  
    char ch[50];  
    InitString(&S1);  
    InitString(&S2);  
    InitString(&Sub);  
    printf("请输入第一个字符串:");  
    gets(ch);  
    StrAssign(&S1,ch);  
    printf("经过赋值操作后的串S1:
");  
    StrPrint(S1);  
    printf("请输入第二个字符串:");  
    gets(ch);  
    StrAssign(&S2,ch);  
    printf("经过赋值操作后的串S2:
");  
    StrPrint(S2);  
    printf("将串S2连接到串S1的末尾,S1串为:
");  
    StrConcat(&S1,S2);  
    StrPrint(S1);  
    printf("经过赋值操作后的串Sub:
");  
    StrAssign(&Sub,"Everyone");  
    StrPrint(Sub);  
    printf("将串S2插入到串S1的第一个位置:
");  
    StrInsert(&S1,1,Sub);  
    StrPrint(S1);  
    printf("把串S1的第一个位置之后的8个字符删除:
");  
    StrDelete(&S1,1,8);  
    StrPrint(S1);  
    printf("将串S1中的S2置换成Sub:
");  
    StrAssign(&Sub,"Xi'an");  
    StrReplace(&S1,S2,Sub);  
    StrPrint(S1);  
    StrDestory(&S1);  
    StrDestory(&S2);  
    StrDestory(&Sub);  
    return 0;  
}  

原文地址:https://www.cnblogs.com/yjbjingcha/p/7277948.html