我想在 C 中将以下 html 打印到控制台。但是,我的代码中有一个错误让我非常困惑,我不知道这怎么会发生?错误是它再次打印最后一个字符数组。
样本输出:
<html>
<body>
<p>Hey There!</p>
<p>You can search for things on the internet at:
<ul>
<li> <a href="http://www.google.com">Google</a></li>
<li> <a href="http://www.bing.com">Bing</a></li>
</ul>
</p>
</body>
</html>
My output:
<html>
<body>
<p>Hey There!</p>
<p>You can search for things on the internet at:
<ul>
<li> <a href="http://www.google.com">Google</a></li>
following line is incorrect. It prints the above line again.
<li> <a href="http://www.bing.com" href="http://www.google.com">Bing</a></li>
</ul>
</p>
</body>
</html>
我的代码:
#include <stdio.h>
#include <stdint.h>
#include <string.h>
void open_tag(char tag[], char attribute[]){
char str1[100];
char str2[100];
char str3[100];
//The bug happens here.The attribute[] is incorrect.
strcpy (str1,tag);
strncat (str1, attribute, 100);
strcpy (str2,"<");
strcpy (str3,">");
strncat (str2, str1, 100);
strncat (str2, str3, 50);
printf ("%s",str2);
}
void close_tag(char tag[]){
char str1[20];
char str2[20];
char str3[20];
strcpy (str1,tag);
strcpy (str2,"</");
strcpy (str3,">");
strncat (str2, str1, 6);
strncat (str2, str3, 6);
printf ("%s",str2);
}
int main(int argc, char *argv[])
{
open_tag("html", "");
printf("\n");
open_tag("body", "");
printf("\n");
open_tag("p", "");
printf("Hey There!");
close_tag("p");
printf("\n");
open_tag("p", "");
printf("You can search for things on the internet at:");
printf("\n");
open_tag("ul", "");
printf("\n");
open_tag("li", "");
char arr2[] = {' ','h','r','e','f','=','\"','h','t','t','p',':','/','/','w','w','w','.','g','o','o','g','l','e','.','c','o','m','\"'};
open_tag("a", arr2);
printf("Google");
close_tag("a");
close_tag("li");
printf("\n");
open_tag("li", "");
char arr1[] = {' ','h','r','e','f','=','\"','h','t','t','p',':','/','/','w','w','w','.','b','i','n','g','.','c','o','m',' ','\"'};
open_tag("a", arr1);
printf("Bing");
close_tag("a");
close_tag("li");
printf("\n");
close_tag("ul");
printf("\n");
close_tag("p");
printf("\n");
close_tag("body");
printf("\n");
close_tag("html");
printf("\n");
return 0;
}