2023每日刷题(三十八)
Leetcode—1410.HTML实体解析器
算法思想
实现代码
typedef struct entityChar {
char* entity;
char rechar;
}entity;
entity matches[] = {
{""", '"'},
{"'", '\''},
{"&", '&'},
{">", '>'},
{"<", '<'},
{"⁄", '/'}
};
char* entityParser(char* text) {
int n = strlen(text);
char* ans = (char *)malloc(sizeof(char) * (n + 10));
char* p = ans;
int flag = 0;
int i = 0;
while(i < n) {
flag = 0;
if(text[i] == '&') {
for(int j = 0; j < sizeof(matches) / sizeof(matches[0]); j++) {
if(strncmp(text + i, matches[j].entity, strlen(matches[j].entity)) == 0) {
strcpy(p, &matches[j].rechar);
p += strlen(&matches[j].rechar);
i += strlen(matches[j].entity);
flag = 1;
break;
}
}
}
if(!flag) {
*p = text[i];
p++;
i++;
}
}
*p = '\0';
return ans;
}
运行结果
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!