【练习】顺序串的实现及应用

1.顺序串的实现

  • 顺序存储结构类型
#define MaxLength 60
typedef struct
{
    char str[MaxLength];
    int length;
}SeqString;
  • 串的赋值
void StrAssign(SeqString *S,char cstr[]) 
/*串的赋值操作*/
{
    int i=0;
    for(i=0;cstr[i]!='\0';i++)  /*将常量cstr中的字符赋值给串S*/
        S->str[i]=cstr[i];
    S->length=i;
}
  • 串的状态
int StrEmpty(SeqString S)   
/*判断串是否为空,串为空返回1,否则返回0*/
{
    if(S.length==0)         /*判断串的长度是否等于0*/
        return 1;           /*当串为空时,返回1;否则返回0*/
    else
        return 0;
}
int StrLength(SeqString S) 
/*求串的长度操作*/
{
    return S.length;
}
  • 串的复制
void StrCopy(SeqString *T,SeqString S) 
/*串的复制操作.*/
{
    int i;
    for(i=0;i<S.length;i++)     /*将串S的字符赋值给串T*/
        T->str[i]=S.str[i];
    T->length=S.length;         /*将串S的长度赋值给串T*/
}
  • 串的比较
int StrCompare(SeqString S,SeqString 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(SeqString *S,int pos,SeqString T)
/*串的插入操作。在S中第pos个位置插入T分为三种情况*/
{
    int i;
    if(pos<0||pos-1>S->length)      /*插入位置不正确,返回0*/
    {
        printf("插入位置不正确");
        return 0;
    }
    if(S->length+T.length<=MaxLength)           /*第一种情况,插入子串后串长≤MaxLength,即子串T完整地插入到串S中*/
    {
        /*在插入子串T前,将S中pos后的字符向后移动len个位置*/
        for(i=S->length+T.length-1;i>=pos+T.length-1;i--)
            S->str[i]=S->str[i-T.length];
        /*将串插入到S中*/
        for(i=0;i<T.length;i++)
            S->str[pos+i-1]=T.str[i];
        S->length=S->length+T.length;
        return 1;
    }
    /*第二种情况,子串可以完全插入到S中,但是S中的字符将会被截掉*/
    else if(pos+T.length<=MaxLength)
    {
        for(i=MaxLength-1;i>T.length+pos-1;i--) /*将S中pos以后的字符整体移动到数组的最后*/
            S->str[i]=S->str[i-T.length];
        for(i=0;i<T.length;i++)                 /*将T插入到S中*/
            S->str[i+pos-1]=T.str[i];
        S->length=MaxLength;
        return 0;
    }
    /*第三种情况,子串T不能被完全插入到S中,T中将会有字符被舍弃*/
    else
    {
        for(i=0;i<MaxLength-pos;i++)            /*将T直接插入到S中,插入之前不需要移动S中的字符*/
            S->str[i+pos-1]=T.str[i];
        S->length=MaxLength;
        return 0;
    }
}
  • 删除子串
int StrDelete(SeqString *S,int pos,int len)
/*在串S中删除pos开始的len个字符*/
{
    int i;
    if(pos<0||len<0||pos+len-1>S->length)
    {
        printf("删除位置不正确,参数len不合法");
        return 0;
    }
    else
    {
        for(i=pos+len;i<=S->length-1;i++)
            S->str[i-len]=S->str[i];
        S->length=S->length-len;
        return 1;
    }
}
  • 串的连接
int StrCat(SeqString *T,SeqString S)
{
    int i,flag;
    if(T->length+S.length<=MaxLength)
    {
        for(i=T->length;i<T->length+S.length;i++)
            T->str[i]=S.str[i-T->length];
        T->length=T->length+S.length;
        flag=1;
    }
    else if(T->length<MaxLength)
    {
        for(i=T->length;i<MaxLength;i++)
            T->str[i]=S.str[i-T->length];
        T->length=MaxLength;
        flag=0;
    }

    return flag;
}
  • 清空串
void StrClear(SeqString *S) 
{
    S->length=0;
}
  • 截取子串
int SubString(SeqString *Sub,SeqString S,int pos,int len)
{
    int i;
    if(pos<0||len<0||pos+len-1>S.length)
    {
        printf("参数pos和len不合法");
        return 0;
    }
    else
    {
        for(i=0;i<len;i++)
            Sub->str[i]=S.str[i+pos-1];
        Sub->length=len;
        return 1;
    }
}
  • 串的替换
int StrReplace(SeqString *S,SeqString T,SeqString V)
{
    int i;
    int flag;
    if(StrEmpty(T))
        return 0;
    i=0;
    do
    {
        i=StrIndex(*S,i,T);
        if(i)
        {
            StrDelete(S,i,StrLength(T));
            flag=StrInsert(S,i,V);
            if(!flag)
                return 0;
            i+=StrLength(V);
        }
    }while(i);
    return 1;
}
  • 查找子串
int StrIndex(SeqString S,int pos,SeqString T) 
{
    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
        {
            i=i-j+1;
            j=0;
        }
    }
    if(j>=T.length)
        return i-j+1;
    else
        return 0;
}
  • 测试示例
    #include<stdio.h>
#include"SeqString.h"
void StrPrint(SeqString S);
void main()
{
    SeqString S1,S2,Sub;
    char ch[MaxLength]; 
    printf("请输入第一个字符串:\n");
    gets(ch);
    StrAssign(&S1,ch);
    printf("输出串S1:");
    StrPrint(S1);
    printf("请输入第二个字符串:\n");
    gets(ch);
    StrAssign(&S2,ch);
    printf("输出串S2:");
    StrPrint(S2);
    printf("将串S2插入到S1的第13个位置:\n");
    StrInsert(&S1,13,S2);
    StrPrint(S1);
    printf("将串S1中的第22个位置起的7个字符删除:\n");
    StrDelete(&S1,22,7);
    StrPrint(S1);
    printf("将串S2中的第6个位置起的4字符取出放入Sub中:\n");
    SubString(&Sub,S2,6,4);
    StrPrint(Sub);
    printf("将串Sub赋值为America:\n");
    StrAssign(&Sub,"America");
    printf("将串S1中的串S2用Sub取代:\n");
    StrReplace(&S1,S2,Sub);
    StrPrint(S1);
}
void StrPrint(SeqString S)
{
    int i;
    for(i=0;i<S.length;i++)
    {
        printf("%c",S.str[i]);
    }
    printf("\n");
}
  • 测试结果

这里写图片描述


2.顺序串应用示例

  下面为一个删除字符串“abcdeabdbcdaaabdecdf”中所有子串“abd”的程序。

  • 源代码
#include<stdio.h>
#include<string.h>
#define MaxLen 60
typedef struct
{
    char str[MaxLen];
    int length;
}SeqString;
int DelSubString(SeqString *S,int pos,int n);
void StrPrint(SeqString S);     /*串的输出函数声明*/
int Index(SeqString *S1,SeqString *S2)
{
    int i=0,j,k;
    while(i<S1->length)
    {
        j=0;
        if(S1->str[i]==S2->str[j])
        {
            k=i+1;
            j++;
            while(k<S1->length && j<S2->length && S1->str[k]==S2->str[j])
            {
                k++;
                j++;
            }
            if(j==S2->length)
                break;
            else
                i++;
        }
        else
            i++;
    }
    if(i>=S1->length)
        return -1;
    else
        return i+1;
}
int DelSubString(SeqString *S,int pos,int n)
{
    int i;
    if(pos+n>S->length)
        return 0;
    for(i=pos+n-1;i<S->length;i++)
        S->str[i-n]=S->str[i];
    S->length=S->length-n;
    S->str[S->length]='\0';
    return 1;
}
int StrLength(SeqString *S)
{
    return S->length;
}
void DelAllString(SeqString *S1,SeqString *S2)
{
    int n;
    n=Index(S1,S2);
    while(n>=0)
    {
        DelSubString(S1,n,StrLength(S2));
        n=Index(S1,S2);
    }
}
void CreateString(SeqString *s,char str[])
{
    strcpy(s->str,str);
    s->length=strlen(str);
}
void main()
{
    SeqString S1,S2;
    char str[MaxLen];
    printf("字符串:");
    gets(str);
    CreateString(&S1,str);
    printf("子串:");
    gets(str);
    CreateString(&S2,str);
    DelAllString(&S1,&S2);
    printf("删除所有子串后的字符串:");
    StrPrint(S1);
}
void StrPrint(SeqString S)
{
    int i;
    for(i=0;i<S.length;i++)
    {
        printf("%c",S.str[i]);
    }
    printf("\n");
}
  • 测试结果

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值