Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Convert below programme Stack to tree in c ( DSA ). #include #include #include #include #define max 20 int top = -1; char s[max][100]; void

Convert below programme Stack to tree in c ( DSA ).

#include

#include

#include

#include

#define max 20

int top = -1;

char s[max][100];

void push(char a[100]) //push function of stack

{

if(top == (max-1))

printf(" Stack Overflow ");

else

strcpy(s[++top] , a);

}

char *pop() //pop function of stack

{

if(top == -1)

printf(" Stack Underflow ");

else

return s[top--];

}

int checktag(char a[]) //this checks the tag name and verifies if it is valid or not

{

int i;

int flag=0;

for(int i=2; i<(strlen(a)-1) ; i++)

{

if(((a[i]>=65 && a[i]<=90) || (a[i]>=97 && a[i]<=122)) && (a[strlen(a)-(i+1)] == '/' || a[strlen(a)-(i+2)] == '<') && (a[strlen(a)-1] == '>'))

flag = 1;

else

flag = 0;

}

return flag;

}

int checkdescription(char a[]) //this verifies the description betwen opening and closing tag and tells us if the text is valid or not

{

for(int i=0 ; i

{

if(a[i] == '<' || a[i] == '>' || a[i] == '&')

return 0;

}

return 1;

}

int checkdupli(char a[]) //checks for duplicate tag

{

for(int i=0; i

{

if(strcmp(a,s[i]) == 0)

return 0;

}

return 1;

}

int check(char a[]) //final check for an xml code

{

int i=0;

char b[20];

if(a[0] == '<') //if starts with '<' it is a xml tag

{

if(checkdupli(a) == 1 && checktag(a) == 1) //checks if tagname is not duplicate and also verfies condition as per xml

{

if(a[1] == '/') //if this is there that means it is closing tag

{

while(top != -1) //while stack has space

{

strcpy(b,pop()); //we pop since we got a closing tag

if(b[0] == '<') //when we get the first character as '<' in the values in stack after poping

{

char c[20]; //defining another string c

int j=0,k=0;

while(strlen(a)>j)

{

if(a[j] != '/')

c[k++] = a[j]; //we store the opening tag name in c

j++;

}

c[j-1] = '\0'; //atlast ending with null

if(strcmp(c,b) == 0) //if both opening and closing tags are matched that means the code is correct

return 1;

}

}

return 0;

}

push(a); //if no closing tag found, we push in the stack

}

else //if we find duplicate tag or dont find tag name as per rules we display following message and exit the code

{

printf("Missing closing tag or you have entered a tag which is not allowed as per xml rules");

exit(0);

}

}

else //if it is not tag name that means it is description between opening and closing tag

{

if(checkdescription(a) == 0) //if description violates the rule, we display the message and exit

{

printf(" You have entered some illegal character in the description between opening and closing tags ");

exit(0);

}

push(a); //else we push the description in the stack

}

return 1;

}

int checkspace(char a[]) //checking for space in between

{

int i=0;

while(a[i] != '\0')

{

if(a[i] == " ")

return 1;

i++;

}

return 0;

}

int split(char a[]) //for spliting

{

int p=1;

char b[100];

strcpy(b,a);

char c[20];

char *t = strtok(b,">"); //splitting the opening tag

if(a[0] == '<')

{

if(a[strlen(a)-1] != '>') //if for eg only found then error generated

{

printf(" Missing closing tag in the end ");

exit(0);

}

}

while(t!=NULL)

{

if(p==1) //for opening tag name

{

char d[100];

strcpy(d,t);

if(checkspace(d) == 1) //if found space in opening tag name

{

int j=0;

while(d[j] != ' ')

{

b[j] = d[j];

}

d[j] = '\0';

snprintf(c,sizeof c , "%s>" , d); //adding '>' to opening tag at last since due to strtok has omitted it while splitting

}

else

snprintf(c,sizeof c , "%s>" , d); //adding '>' to opening tag at last since due to strtok has omitted it while splitting

}

else if(p==3) //for closing tag

{

snprintf(c,sizeof c , "<%s" , t); //adding '<' to closing tag at first since due to strtok has omitted it while splitting

}

else

strcpy(c,t); //else we copy the desciption directly which is between opening and closing tag

if(check(c) == 0) //we check the whole array now if something is not according to rules

{

printf(" Missing opening tag with respect to closing tag enetered of same tag name ");

exit(0);

}

t = strtok(NULL,"<"); //else we split again, this time description and end tag

p++;

}

return 1;

}

void main() //main method

{

int n;

FILE *f; //File f

f = fopen("xml_sample.xml","w"); //we do write method as if file not found it creates automatically

printf(" Enter the number of lines in your code "); //asking number of lines in code

scanf("%d",&n);

char a[n][100];

char s[100];

printf(" Enter the XML Declaration "); //asking for xml version declaration which is stated in below comment

gets(s);

gets(s); //

fputs(s,f);

fputs(" ",f);

printf(" Write your XML code line by line "); //now writing the code

for(int i=0; i

{

gets(a[i]); //scanning it

if(split(a[i]) == 0) //splitting the code as per opening tag, description and closing tag and if any error found, printing the message

{

printf(" Missing opening tag value ");

exit(0);

}

fputs(a[i],f); //after verifying we put it in the file

fputs(" ",f);

}

fclose(f); //after above loop is completed that is code written successfully, we close the file

printf(" Written Successfully ");

}

/*

INPUT AND OUTPUT-1

Enter the number of lines in your code

3

Enter the XML Declaration

Write your XML code line by line

ello

Missing closing tag or you have entered a tag which is not allowed as per xml rules

INPUT AND OUTPUT-2

Enter the number of lines in your code

3

Enter the XML Declaration

Write your XML code line by line

ello

Written Successfully

*/

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions