這是沒有內存限制:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/* ---------------------------------------------------------------------------
Name : replace - Search & replace a substring by another one.
Creation : Thierry Husson, Sept 2010
Parameters :
str : Big string where we search
oldstr : Substring we are looking for
newstr : Substring we want to replace with
count : Optional pointer to int (input/output value). NULL to ignore.
Input: Maximum replacements to be done. NULL or < 1 to do all.
Output: Number of replacements done or -1 if not enough memory.
Returns : Pointer to the new string or NULL if error.
Notes :
- Case sensitive - Otherwise, replace functions "strstr" by "strcasestr"
- Always allocate memory for the result.
--------------------------------------------------------------------------- */
char* replace(const char *str, const char *oldstr, const char *newstr, int *count)
{
const char *tmp = str;
char *result;
int found = 0;
int length, reslen;
int oldlen = strlen(oldstr);
int newlen = strlen(newstr);
int limit = (count != NULL && *count > 0) ? *count : -1;
tmp = str;
while ((tmp = strstr(tmp, oldstr)) != NULL && found != limit)
found++, tmp += oldlen;
length = strlen(str) + found * (newlen - oldlen);
if ((result = (char *)malloc(length+1)) == NULL) {
fprintf(stderr, "Not enough memory\n");
found = -1;
} else {
tmp = str;
limit = found; /* Countdown */
reslen = 0; /* length of current result */
/* Replace each old string found with new string */
while ((limit-- > 0) && (tmp = strstr(tmp, oldstr)) != NULL) {
length = (tmp - str); /* Number of chars to keep intouched */
strncpy(result + reslen, str, length); /* Original part keeped */
strcpy(result + (reslen += length), newstr); /* Insert new string */
reslen += newlen;
tmp += oldlen;
str = tmp;
}
strcpy(result + reslen, str); /* Copies last part and ending nul char */
}
if (count != NULL) *count = found;
return result;
}
/* ---------------------------------------------------------------------------
Samples
--------------------------------------------------------------------------- */
int main(void)
{
char *str, *str2;
int rpl;
/* ---------------------------------------------------------------------- */
/* Simple sample */
rpl = 0; /* Illimited replacements */
str = replace("Hello World!", "World", "Canada", &rpl);
printf("Replacements: %d\tResult: [%s]\n\n", rpl, str);
/* Replacements: 1 Result: [Hello Canada!] */
free(str);
/* ---------------------------------------------------------------------- */
/* Sample with dynamic memory to clean */
rpl = 0; /* Illimited replacements */
str = strdup("abcdef");
if ((str2 = replace(str, "cd", "1234", &rpl)) != NULL) {
free(str);
str = str2;
}
printf("Replacements: %d\tResult: [%s]\n\n", rpl, str);
/* Replacements: 1 Result: [ab1234ef] */
free(str);
/* ---------------------------------------------------------------------- */
/* Illimited replacements - Case sensitive & Smaller result */
str = replace("XXXHello XXXX world XX salut xxx monde!XXX", "XXX", "-",NULL);
printf("Result: [%s]\n\n", str);
/* Result: [-Hello -X world XX salut xxx monde!-] */
free(str);
/* ---------------------------------------------------------------------- */
rpl = 3; /* Limited replacements */
str = replace("AAAAAA", "A", "*", &rpl);
printf("Replacements: %d\tResult: [%s]\n\n", rpl, str);
/* Replacements: 3 Result: [***AAA] */
free(str);
return 0;
}
您是否有興趣在一個通用的解決方案,與DEST,其中src和dest中可以有不同的長度SRC替換所有出現的? – Ashwin 2009-09-29 21:48:40
是的,長度無關緊要。 – user105033 2009-09-29 21:51:55