C语言实现字符串替换函数str_replace()和删除两端空格函数trim()

一、字符串替换
用法1:直接在字符数组里面完成。
str_replace(要替换的内容, 替换后的内容, 原始字符串所在的字符数组, 字符数组的大小);
例如
char str[100];
strcpy(str, "this is good");
str_replace("is", "*", str, sizeof(str));
用法2:另外分配空间存放新字符串。
新字符串 = str_replace(要替换的内容, 替换后的内容, 原始字符串, -1);
例如
char *s = str_replace("is", "*", "this is good", -1);
if (s != NULL)
{
    printf("%s\n", s);
    free(s);
}

二、删除两端空格
trim(字符数组);
例如
char str[] = "     this     ";
int len = trim(str);
printf("%s\n", s);

#include <stdlib.h>
#include <string.h>

char *str_replace(const char *search, const char *replace, char *subject, int bufsize)
{
    char *p;
    int i, j = 0, n = 0;
    int len, slen, rlen, tlen;
    
    // check arguments
    if (subject == NULL)
    {
        return NULL;
    }
    tlen = strlen(subject);
    if (bufsize == -1)
    {
        // make sure subject is writable
        p = malloc(tlen + 1);
        if (p == NULL)
        {
            return NULL;
        }
        memcpy(p, subject, tlen + 1);
        subject = p;
    }
    if (search == NULL || search[0] == '\0')
    {
        return subject;
    }
    slen = strlen(search);
    if (replace == NULL)
    {
        replace = "";
    }
    rlen = strlen(replace);

    // find out how many replacements are needed
    for (i = 0; subject[i] != '\0'; i++)
    {
        if (subject[i] != search[j])
        {
            // character mismatch
            j = 0;
        }
        if (subject[i] == search[j])
        {
            j++;
            if (j == slen)
            {
                // pattern match
                subject[i + 1 - j] = '\0'; // front
                subject[i] = '\0'; // rear
                j = 0;
                n++;
            }
        }
    }
    if (n == 0)
    {
        return subject;
    }

    // calculate the length of the new string and check buffer size
    len = tlen + n * (rlen - slen);
    if (bufsize == -1)
    {
        if (len > tlen)
        {
            // increase buffer size
            p = realloc(subject, len + 1);
            if (p == NULL)
            {
                free(subject);
                return NULL;
            }
            subject = p;
        }
    }
    else if (bufsize < len + 1)
    {
        return NULL;
    }

    // replace substrings
    if (len <= tlen)
    {
        i = 0; // for read
        j = 0; // for write
        while (i < tlen)
        {
            if (subject[i] == '\0')
            {
                memcpy(subject + j, replace, rlen);
                i += slen;
                j += rlen;
            }
            else
            {
                subject[j] = subject[i];
                i++;
                j++;
            }
        }
    }
    else
    {
        i = tlen - 1;
        j = len - 1;
        while (i >= 0)
        {
            if (subject[i] == '\0')
            {
                memcpy(subject + j + 1 - rlen, replace, rlen);
                i -= slen;
                j -= rlen;
            }
            else
            {
                subject[j] = subject[i];
                i--;
                j--;
            }
        }
    }
    subject[len] = '\0';
    return subject;
}

int trim(char *s)
{
    int i, j = -1, len = 0;

    for (i = 0; s[i] != '\0'; i++)
    {
        if (j == -1 && s[i] != ' ' && s[i] != '\t')
        {
            j = 0;
        }
        if (j != -1)
        {
            s[j] = s[i];
            j++;
            if (s[i] != ' ' && s[i] != '\t')
            {
                len = j;
            }
        }
    }
    s[len] = '\0';
    return len;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

巨大八爪鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值