Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You are asked to write a program to check if the HTML tags in a given file are n

ID: 3551998 • Letter: Y

Question

You are asked to write a program to check if the HTML tags in a given file are nested correctly or not. This problem is similar to the example of proper matching of { ( [ ] ) }, but now you will deal with HTML tags and read them from a file. You need to implement and use use stack lib that we discussed in class with typedef void *stackElementT; or typedef char *stackElementT;


For example, if an HTML file contains

< title ><b> THIS FILE </b> USES CORRECTLY NESTED TAGS </title>

< h1 ><i> First <b class="c1"> header < /b > text <img src="pic.jpg" /> < /i ></h1>

< p id=par1" > Some other text < /p >


Then YES, all the tags are nested correctly.


But if an HTML file contains

< title> < b> THIS FILE < /title > IS < /b > NOT NESTED CORRECTLY .

< p > < b > some text is not nested correctly < /b >


Then NO, the tag < B > < /title > violates the proper nesting!



Your program must accept HTML input file name as a command line argument and process it for proper nesting of HTML tags. You can stop the program when you detect the first tag that violates the proper nesting structure and print NO and the tag that violates nesting on the screen. Otherwise, your program will print YES, all the tags are nested correctly.

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <ctype.h>

#define STACK_MAX 100

#define TAG_MAX 50



struct Stack {

char data[STACK_MAX][TAG_MAX];

int size;

};

//typedef struct Stack Stack;



void Stack_Init(Stack *S)

{


S->size = 0;

}


char* Stack_Top(Stack *S)

{

if (S->size == 0) {

fprintf(stderr, "Error: stack empty ");

char* a;

return a;

}


return S->data[S->size-1];

}


void Stack_Push(Stack *S, char d[])

{

if (S->size < STACK_MAX)

strcpy(S->data[S->size++],d);

else

fprintf(stderr, "Error: stack full ");

}


void Stack_Pop(Stack *S)

{

if (S->size == 0)

fprintf(stderr, "Error: stack empty ");

else

S->size--;

}


bool strcompare(char* s1, char* s2){

int i=0;

while(s1[i]!='' && s2[i]!=''){

if(s1[i]!=s2[i]) return false;

else i++;

}


if(s1[i]!='' || s2[i]!='') return false;

else return true;

}


char* striptag(char* tag){

int firstflag=0;

for(int i=0;tag[i]!='';i++){

if(tag[i]==' ' && firstflag!=0){

tag[i]='';

break;

}

else if(tag[i]!=' ') firstflag=1;

}


return tag;

}


int main(){


char fileName[50];

printf("enter html file name: ");

scanf("%s", fileName);

struct Stack temp;

temp.size = 0;


struct Stack* S = &temp;



FILE *file;

char c ;

int tagFlag = 0; //0 for text 1 for open, 2 for close

char tag[TAG_MAX];

memset(tag, 0, sizeof tag);

int tagsize=0;

int firstflag=0;

//memset(members, 0, sizeof members);


file = fopen(fileName, "r");

do

{

  

c= getc(file);



if(c=='<'){

tagFlag=1;

}

else if(c=='>'){

if(strcompare(striptag(tag),(char*) "img") || strcompare(striptag(tag),(char*) "br")){

//printf("%s ", tag);

memset(tag, 0, sizeof tag);

tagsize=0;

tagFlag=0;

firstflag=0;

}

else if(tagFlag==1){

Stack_Push(S, striptag(tag));

//printf("%s ", tag);

memset(tag, 0, sizeof tag);

tagsize=0;

tagFlag=0;

firstflag=0;

}

else if(tagFlag==2){

if(strcompare(striptag(tag),Stack_Top(S))){

Stack_Pop(S);

memset(tag, 0, sizeof tag);

tagsize=0;

tagFlag=0;

firstflag=0;

}

else{

printf("tags <%s> and </%s> violates the proper nesting ", tag, Stack_Top(S) );

exit(0);

}

}

}

else if(c==' '){

if(firstflag!=0){

tag[tagsize++]=tolower(c);

}

}

else if(c=='/' || c=='\'){

if(tagFlag==1) tagFlag=2;

}

else if(tagFlag==0){

;

}

else if(tagFlag==1 || tagFlag==2){

tag[tagsize++]=tolower(c);

firstflag=1;

}


} while(c != EOF);


if(S->size!=0){

printf("Tag not matched for <%s> ",Stack_Top(S));

}

else printf("All tags matched ");



}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote