厦大C语言上机 1491 子串删除

1491.子串删除


时间限制: 1000 MS          内存限制: 65536 K
        
提交数: 400 (0 users)          通过数: 211 (203 users)


问题描述
   给定两个字符串s和t,若s是t的子串,将t中的子串s删除,若存在多个子串,则全部删除;若s不是t的子串,对字符串t不做处理。字符串s和t长度不超过1000。


输入格式
第一行,字符串t,文本长度<=1000。
第二行,字符串s,文本长度<=1000。


输出格式
处理后的字符串t


样例输入
Hello World!
  Hello
Hello World!
  Ho
No pain, no gain
  ain


样例输出
   World! 
Hello World!
No p, no g


来源

xmu

#include <stdio.h>
#include <string.h>

void delete_substring(char *t, char *s, char *output)
{
    int len_t = (int)strlen(t);
    int len_s = (int)strlen(s);
    int ptr_t, ptr_s, ptr_output, ptr_temp;

    ptr_t = 0;
    ptr_output = 0;
    while (ptr_t < len_t)
    {
        ptr_temp = ptr_t;
        ptr_s = 0;
        while (ptr_temp < len_t && ptr_s < len_s)
        {
            if (t[ptr_temp] != s[ptr_s])
                break;
            ptr_temp++;
            ptr_s++;
        }
        if (ptr_s == len_s)
            ptr_t += len_s;
        else
            output[ptr_output++] = t[ptr_t++];
    }
    output[ptr_output] = '\0';
}

int main()
{
    char t[1005] = { 0 };
    char s[1005] = { 0 };
    char output[1005] = { 0 };

    while (gets(t))
    {
        gets(s);
        delete_substring(t, s, output);
        printf("%s\n", output);
    }

    return 0;
}


  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值