如何在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