删除字符串中的子串(返回子串)

   条件:()

  算法思想:首先找到你需要的子串的第一个位置i,找到了之后再用while循环确定子串的长度(len)。最后将最后一个结点的指针区间置为NULL;

程序:

#include <stdio.h>
#include <stdlib.h>

typedef char datatype;
typedef struct node{
    datatype data;
    struct node *next;
}seqstring;

// 字符串的链式存储的创建
seqstring * creat()
{
    seqstring *s,*p=NULL,*head=NULL;
    datatype ch;
    while((ch=getchar())!='\n')
    {
        s=(seqstring *)malloc(sizeof(seqstring));
        s->data=ch;
        if(head==NULL)
            head=p=s;
        else
        {
            p->next=s;
            p=s;
        }
        p->next=NULL;
    }
    return head;
}

//  字符串的遍历

void display(seqstring *head)
{
   seqstring *p=head;
   while(p)
   {
       printf("%c",p->data);
       p=p->next;
   }
   printf("\n");
}

//
seqstring * subsititute(seqstring *head,int i,int len)
{
    seqstring *p=head,*r,*q,*s;
    int k=1;
    while(p && k!=i)
    {
        p=p->next;
        k++;
    }
    if(!p)
    {
        printf("ERROR \n");
        return 0;
    }
    else
    {
        k=1;
       r=(seqstring *)malloc(sizeof(seqstring));
       r->data=p->data;
       r->next=NULL;
       q=r;
     while(p->next && k<len)
      {
        p=p->next;
        s=(seqstring *)malloc(sizeof(seqstring));
        s->data=p->data;
        k++;
        q->next=s;
        q=s;
     }
    if(k<len)
       {
          printf("ERROR 2\n");
          return 0;
       }
    else
        {
           q->next=NULL;
           return r;
        }
    }
}

int main()
{
    int i,len;
    seqstring *head,*str2;
    head=creat();
    display(head);
    scanf("%d",&i);
    scanf("%d",&len);
    str2=subsititute(head,i,len);
    display(str2);
    return 0;

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值