如何在C中执行搜索和替换?我试图做功能来取代字符串中的HTML实体。我已经有了函数来查找html实体的开始和结束,但我无法弄清楚如何替换它们。在字符串中搜索并替换
这里是我已经:
#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;
}
我明白了。非常感谢你! :-) – 2014-11-09 14:16:09
不客气!很高兴它的工作。 – Laird 2014-11-09 14:33:50