Reputation: 606
How can i do search and replace in C? I'm trying to do function to replace html entities in string. I already have the function to find beginning and end of html entities but i can't figure out how to replace them.
Here is what i have already:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct entity {
char *entity;
char *substitute;
};
void replacehtmlentities(char *str, char *dest) {
int i;
char *begin = NULL;
char *end;
struct entity entities[] = {
{ " ", " " },
{ "<", "<" },
{ ">", ">" },
{ "&", "&" },
{ "€", "€" },
{ "©", "©" },
{ "®", "®" },
{ NULL, NULL },
};
for (i = 0; entities[i].entity; i++) {
while (begin = strstr(str, entities[i].entity)) {
end = begin + strlen(entities[i].entity);
// how to replace
}
}
}
int main(int argc, char **argv) {
char *str = "space lowerthan < end";
printf("%s\n", str);
replacehtmlentities(str);
printf("%s\n", str);
return EXIT_SUCCESS;
}
Upvotes: 0
Views: 175
Reputation: 138
The short answer is to use an existing string replacement function. There's one on my site at http://creativeandcritical.net/str-replace-c/ (the current version is named replace_str2
). The changes you'd need to make to your code to use it (tested) are:
#include <stddef.h>
to the other includes.replace_str2
function into the file above the replacehtmlentities
function.Change the prototype of the function replacehtmlentities
to:
char *replacehtmlentities(char *str)
Add to that function the following variable declarations:
char *tmp = NULL;
char *tmp2 = str;
Replace in that function the code:
while (begin = strstr(str, entities[i].entity)) {
end = begin + strlen(entities[i].entity);
// how to replace
}
with:
tmp = replace_str2(tmp2, entities[i].entity, entities[i].substitute);
if (i) free(tmp2);
tmp2 = tmp;
Add a final return to that function:
return tmp2;
In main, change your call of that function to:
str = replacehtmlentities(str);
As an additional note: in main, str will now reference memory allocated with malloc. If/when you no longer require this string, you can free the memory by calling free(str).
Upvotes: 2
Reputation: 409196
The pointer str
points to a string literal, and string literals are read only (i.e. constant). Trying to modify a string literal will lead to undefined behavior.
The solution is very simple: Declare str
as an array:
char str[] = "space lowerthan < end";
However be careful when replacing sequences in a string, so you don't replace a shorter sub-string with a longer, as then you might write beyond the end of the string.
Upvotes: 2