希望你能给我一些启示:
有这个条目:
BODY
10
<><BODY garbage>body</BODY>
所以我必须替换&lt;中的所有标签。 &GT;并使输出看起来像这样:
<><10 garbage>body</10>
任何想法? 谢谢!
答案 0 :(得分:0)
#include <stdio.h>
#include <string.h>
int main (void){
char tag[128];//enough?
char tag_r[128];
char text[1024] = "";//enough? or dynamic array like as vector
int text_pos = 0;
int ch;
enum { OUT, IN };
int status = OUT;
scanf("%127s %127[^\n]%*c", tag, tag_r);
while((ch = getchar())!=EOF){
if(status == OUT){
putchar(ch);
if(ch == '<')
status = IN;
} else {//if(status == IN){
if(ch == '>'){
size_t len = strcspn(text, " \t\n");
if(len && strncmp(text, tag, len)==0){//match start tag(insufficient rigor), ignore case?
printf("%s", tag_r);
printf("%s", text + len);
} else if(text[0]=='/' && strcmp(text+1, tag)==0){//match end tag
printf("/%s", tag_r);
} else {
printf("%s", text);
}
text[text_pos=0]=0;
putchar(ch);
status = OUT;
} else {
text[text_pos++] = ch;//check omitted
text[text_pos] = 0;
}
}
}
if(text_pos)//invalid syntax
printf("%s", text);
return 0;
}