10.请指出下面代码中的错误 char *encode_html(char *html_string) { int i, dst_index; if (!html_string) return NULL; char *dst buf = (char *)malloc(4 * sizeof (char) * MAX_SIZE); if (MAX_SIZE <= strlen (html_string)) { LOGE ("user string too long, die evil hacker!"); return NULL; } dst_index = 0; for (i =0; i< strlen(html_string);i++) { if ('&'== html_string[i]) { dst_buf[dst_index++] = '&'; dst_buf[dst_index++] = 'a'; dst_buf[dst_index++] = 'm'; dst buf[dst_index++] = 'p'; dst buf[dst_index++] = 'j'; } else if ('<' ==html_string[i]){ /* < Wi11 encoded to &1t; */ } /* other special character */ else dst_buf[dst_index++] = html_string[i]; } return dst_buf; }
时间: 2023-12-03 07:46:07 浏览: 27
下面是代码中的错误:
1. 在第6行中,char *dst buf应该是char *dst_buf,中间应该有一个下划线。
2. 在第8行中,sizeof(char)是1,因此可以省略,所以应该是malloc(4 * MAX_SIZE)。
3. 在第12行中,for循环的右括号中应该是一个分号而不是一个中文的顿号。
4. 在第15行中,dst_buf应该是dst_buf。
5. 在第18行中,应该将注释替换为将"<"编码为"<"。
6. 在第22行中,应该将注释替换为将">"编码为">"。
修改后的代码如下:
```
char *encode_html(char *html_string) {
int i, dst_index;
if (!html_string)
return NULL;
char *dst_buf = (char *)malloc(4 * MAX_SIZE);
if (MAX_SIZE <= strlen(html_string)) {
LOGE("user string too long, die evil hacker!");
return NULL;
}
dst_index = 0;
for (i = 0; i < strlen(html_string); i++) {
if ('&' == html_string[i]) {
dst_buf[dst_index++] = '&';
dst_buf[dst_index++] = 'a';
dst_buf[dst_index++] = 'm';
dst_buf[dst_index++] = 'p';
dst_buf[dst_index++] = ';';
} else if ('<' == html_string[i]) {
dst_buf[dst_index++] = '&';
dst_buf[dst_index++] = 'l';
dst_buf[dst_index++] = 't';
dst_buf[dst_index++] = ';';
} else if ('>' == html_string[i]) {
dst_buf[dst_index++] = '&';
dst_buf[dst_index++] = 'g';
dst_buf[dst_index++] = 't';
dst_buf[dst_index++] = ';';
} else {
dst_buf[dst_index++] = html_string[i];
}
}
return dst_buf;
}
```