串的基本用法(顺序存储结构)

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define  MAXSIZE   10
#define  DataType  char

typedef struct
{
    DataType ch[MAXSIZE];
    int      length;
}SString;

SString  *CreatString(void);
int      GetLength(SString *S);
int      SStrInsert(SString *S,int pos,const SString *T);
int      SStrDelete(SString *S,int pos,int   len);
int      SStrCat(SString *S,const SString *T);
int      SubSString(SString *T,SString *S,int pos,int len);
void     PrintString(SString *S);
void     Destroy(SString *S);

SString  *CreatString(void)
{
    SString *S=NULL;

    S=(SString *)malloc(sizeof(SString));
    S->length=0;
    scanf("%s",S->ch);
    S->length=GetLength(S);

    return S;
}

int      GetLength(SString *S)
{
    int i=0;
    while(S->ch[i]!='\0')
    {
        i++;
    }
    return i;
}

int   SStrInsert(SString *S,int pos,const SString *T)
{
    int length_T=GetLength(T);
    int length_S=GetLength(S);
    int i=0;
    int j=0,k=0,p=0,q=0;

    if(pos<=0||pos>=MAXSIZE)
    {
        printf("Illegal insert location.\n");
        return 1;
    }
    else
    {
            i=pos-1+length_T;
            k=pos-1;
        if(length_S+length_T<=MAXSIZE)
        {
            p=length_S-pos+1;
            q=length_T;
        }
        else if(length_T+pos<=MAXSIZE)
        {
            q=length_T;
            p=MAXSIZE-length_T-pos+1;
        }
        else
        {
            q=MAXSIZE-pos+1;
            p=0;
        }
        for(j=0;j<p;j++)
        {
                S->ch[i+p-1]=S->ch[k+p-1];
                i--;
                k--;
        }
        i=pos-1;
        k=0;

        for(j=0;j<q;j++)
        {
                S->ch[i]=T->ch[k];
                i++;
                k++;
        }
        S->length=GetLength(S);
    }
    return 0;
}

int   SStrDelete(SString *S,int pos,int   len)
{
 int i=0;
 int length=0;
 int k=0;

  if(pos<=0 || pos>GetLength(S) || NULL==S || len<=0 || len>GetLength(S))
  {
      printf("Illeagel input.The program ends.\n");
      exit(0);
  }
  else
  {
      if(pos+len<=MAXSIZE)
          length=len;
      else
      {
          length=GetLength(S)-pos-1;
      }
      k=pos-1;
      for(i=0;i<length;i++)
      {
          S->ch[k]=S->ch[k+len];
          k++;
      }
      S->length=GetLength(S);
      return 0;
  }
}

int   SStrCat(SString *S,const SString *T)
{
  int  pos=GetLength(S);
  int  i=0;
  for(i=0;i<GetLength(T);i++)
  {
      if(pos==MAXSIZE)
          break;
      else
      {
      S->ch[pos]=T->ch[i];
      pos++;
      }
  }
  S->length=GetLength(S);
  return 0;
}

int   SubSString(SString *T,SString *S,int pos,int len)
{
 int i=0;
 int k=pos-1;

  if(pos<=0 || pos>=GetLength(S) || NULL==S || len<=0 || len>GetLength(S))
  {
      printf("Illeagel input.The program ends.\n");
      exit(0);
  }
  else
  {
      for(i=0;i<len;i++)
      {
        if(k==GetLength(S))
        {
          break;
        }
        else
       {
        printf("%c ",S->ch[k]);
        k++;
       }
     }
     return 0;
  }
}

void  PrintString(SString *S)
{
      int i=0;
        while(S->ch[i]!='\0')
      {
        printf("%c",S->ch[i]);
        i++;
      }
      printf("\n");

}

void  Destroy(SString *S)
{
      free(S);
      S=NULL;
}

int main()
{
     SString *s,*t;
     SString *t_2,*t_3=NULL ;


     printf("Please Input a string ,the max length is %d.\n",MAXSIZE);
     s=CreatString();
     printf("Please Input a waiting for being  inserted string.\n");
     t=CreatString();
     SStrInsert(s,5,t);
     printf("After insert a string,the new string is:\n");
     PrintString(s);
     SStrDelete(s,4,4);
     printf("After delect a string,the new string is:\n");
     PrintString(s);
     printf("Please input a string that needs to be chained.\n");
     t_2=CreatString();
     SStrCat(s,t_2);
     printf("After chaining the two strings,the new strings is:\n");
     PrintString(s);
     SubSString(t_3,s,3,4);
     Destroy(s);
     return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值