在字串中查找子串 `"apple"` 並替換為 `"orange"`,原地修改或生成新字串。
void replace_inplace(char *str, const char *oldSub, const char *newSub) {
char *pos = strstr(str, oldSub);
if (!pos) return;
size_t oldLen = strlen(oldSub);
size_t newLen = strlen(newSub);
size_t tailLen = strlen(pos + oldLen);
if (newLen > oldLen) {
// 向後移動尾部騰出空間 (需確保緩衝區足夠)
memmove(pos + newLen, pos + oldLen, tailLen + 1); // +1 複製 \0
} else {
// 向前移動或原地覆蓋
memmove(pos + newLen, pos + oldLen, tailLen + 1);
}
memcpy(pos, newSub, newLen);
}
char* replace_new(const char *str, const char *oldSub, const char *newSub) {
if (!str || !oldSub || !newSub) return NULL;
size_t oldLen = strlen(oldSub);
if (oldLen == 0) return strdup(str); // 避免無限循環
// 1. 計算出現次數
int count = 0;
const char *tmp = str;
while ((tmp = strstr(tmp, oldSub))) {
count++;
tmp += oldLen;
}
if (count == 0) return strdup(str);
// 2. 分配新緩衝區
size_t newLen = strlen(newSub);
size_t resultLen = strlen(str) + count * (newLen - oldLen);
char *result = malloc(resultLen + 1);
if (!result) return NULL;
// 3. 複製並替換
char *out = result;
const char *in = str;
while (*in) {
if (strncmp(in, oldSub, oldLen) == 0) {
memcpy(out, newSub, newLen);
out += newLen;
in += oldLen;
} else {
*out++ = *in++;
}
}
*out = '\0';
return result; // 調用者負責 free
}
#include
std::string replace_cpp(std::string str, const std::string& oldSub, const std::string& newSub) {
size_t pos = 0;
while ((pos = str.find(oldSub, pos)) != std::string::npos) {
str.replace(pos, oldSub.length(), newSub);
pos += newSub.length(); // 避免重複匹配新插入的內容
}
return str;
}
| 輸入 | oldSub | newSub | 預期輸出 | 關鍵點 |
|---|---|---|---|---|
| `"apple apple"` | `"apple"` | `"orange"` | `"orange orange"` | 多次替換 |
| `"pineapple"` | `"apple"` | `"orange"` | `"pinorange"` | 子串重疊/邊界 |
| `"appapple"` | `"apple"` | `"orange"` | `"apporange"` | 部分匹配 |
| `""` | `"a"` | `"b"` | `""` | 空字串 |
| `"aaa"` | `"aa"` | `"b"` | `"ba"` | 重疊替換策略 (左most優先) |