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;
}
運行結果
之后我會持續更新,如果喜歡我的文章,請記得一鍵三連哦,點贊關注收藏,你的每一個贊每一份關注每一次收藏都將是我前進路上的無限動力 !!!↖(▔▽▔)↗感謝支持!