C语言如何替换字符串中的特定子串?

99ANYc3cd6
预计阅读时长 17 分钟
位置: 首页 C语言 正文

方法 1:使用 strncpystrcat 手动替换

这种方法的基本思路是:

替换字符串中的字符串 c语言
(图片来源网络,侵删)
  1. 找到子字符串的起始位置。
  2. 将子字符串之前的部分复制到新字符串中。
  3. 将替换字符串复制到新字符串中。
  4. 将子字符串之后的部分复制到新字符串中。

代码示例

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* replaceSubstring(const char* str, const char* oldSub, const char* newSub) {
    // 1. 检查输入是否有效
    if (str == NULL || oldSub == NULL || newSub == NULL) {
        return NULL;
    }
    // 2. 计算字符串长度
    size_t strLen = strlen(str);
    size_t oldLen = strlen(oldSub);
    size_t newLen = strlen(newSub);
    if (oldLen == 0) {
        return strdup(str); // oldSub 为空,直接返回原字符串的副本
    }
    // 3. 计算新字符串的最大可能长度
    size_t maxNewLen = strLen - oldLen + newLen + 1;
    // 4. 分配内存
    char* result = (char*)malloc(maxNewLen);
    if (result == NULL) {
        return NULL;
    }
    // 5. 查找子字符串
    const char* found = strstr(str, oldSub);
    if (found == NULL) {
        strcpy(result, str); // 如果没找到,直接返回原字符串
        return result;
    }
    // 6. 复制子字符串之前的部分
    size_t beforeLen = found - str;
    strncpy(result, str, beforeLen);
    result[beforeLen] = '\0';
    // 7. 拼接替换字符串
    strcat(result, newSub);
    // 8. 拼接子字符串之后的部分
    strcat(result, found + oldLen);
    return result;
}
int main() {
    const char* str = "Hello, world! Welcome to the world of C programming.";
    const char* oldSub = "world";
    const char* newSub = "universe";
    char* replaced = replaceSubstring(str, oldSub, newSub);
    if (replaced != NULL) {
        printf("Original: %s\n", str);
        printf("Replaced: %s\n", replaced);
        free(replaced); // 记得释放内存
    }
    return 0;
}

输出

Original: Hello, world! Welcome to the world of C programming.
Replaced: Hello, universe! Welcome to the universe of C programming.

方法 2:逐个字符替换(适用于单字符替换)

如果只是替换单个字符(而不是子字符串),可以使用更简单的方法:

代码示例

#include <stdio.h>
#include <string.h>
void replaceChar(char* str, char oldChar, char newChar) {
    if (str == NULL) {
        return;
    }
    for (int i = 0; str[i] != '\0'; i++) {
        if (str[i] == oldChar) {
            str[i] = newChar;
        }
    }
}
int main() {
    char str[] = "Hello, world! Welcome to the world of C programming.";
    replaceChar(str, 'o', 'O');
    printf("Replaced: %s\n", str);
    return 0;
}

输出

Replaced: HellO, wOrld! WelcOme tO the wOrld Of C prOgramming.

方法 3:使用 sprintf 动态分配内存(适用于多次替换)

如果字符串中可能出现多次子字符串,可以循环替换:

代码示例

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* replaceAll(const char* str, const char* oldSub, const char* newSub) {
    if (str == NULL || oldSub == NULL || newSub == NULL) {
        return NULL;
    }
    size_t oldLen = strlen(oldSub);
    size_t newLen = strlen(newSub);
    size_t strLen = strlen(str);
    if (oldLen == 0) {
        return strdup(str);
    }
    // 计算新字符串的最大可能长度
    size_t maxNewLen = strLen + 1; // 初始估计
    char* result = (char*)malloc(maxNewLen);
    if (result == NULL) {
        return NULL;
    }
    const char* src = str;
    char* dest = result;
    while (1) {
        const char* found = strstr(src, oldSub);
        if (found == NULL) {
            strcpy(dest, src);
            break;
        }
        // 复制子字符串之前的部分
        size_t beforeLen = found - src;
        strncpy(dest, src, beforeLen);
        dest += beforeLen;
        // 拼接替换字符串
        strcpy(dest, newSub);
        dest += newLen;
        src = found + oldLen;
    }
    return result;
}
int main() {
    const char* str = "world world world";
    const char* oldSub = "world";
    const char* newSub = "earth";
    char* replaced = replaceAll(str, oldSub, newSub);
    if (replaced != NULL) {
        printf("Original: %s\n", str);
        printf("Replaced: %s\n", replaced);
        free(replaced);
    }
    return 0;
}

输出

Original: world world world
Replaced: earth earth earth

注意事项

  1. 内存管理:使用 malloc 分配的内存必须用 free 释放,避免内存泄漏。
  2. 字符串长度:确保新字符串不会超出分配的内存空间。
  3. 空指针检查:始终检查输入指针是否为 NULL,避免程序崩溃。
  4. strstr 的局限性strstr 只能查找第一个匹配项,如果需要全局替换,需要循环调用。

方法 适用场景 优点 缺点
方法 1(strncpy + strcat 替换第一次出现的子字符串 简单直接 只能替换第一次出现
方法 2(逐个字符替换) 替换单个字符 高效 不适用于子字符串
方法 3(循环替换) 替换所有出现的子字符串 全局替换 代码稍复杂

选择合适的方法取决于你的具体需求,如果只是简单替换,方法 1 足够;如果需要全局替换,方法 3 更合适。

替换字符串中的字符串 c语言
(图片来源网络,侵删)
-- 展开阅读全文 --
头像
dede如何修改新添加的变量?
« 上一篇 02-12
dede如何获取当前栏目id?
下一篇 » 02-12

相关文章

取消
微信二维码
支付宝二维码

目录[+]