#HTML tag validator using stack.. How to implement that ?

59 messages · Page 1 of 1 (latest)

upper peak
#

Hello
I have a task to check if an html tag is valid
but honestly I am stuck on this for hours without knowing what to do exactly. How to compare the opening tag with closing tag? there are many possibilities that may arise for closing tag and hardcoding them in if statments obviously wouldnt work..

May you pls guide me ?
Thanks in advance

Implement a function that checks if a given HTML-like string has properly nested tags.
Use a stack for the solution. Tags appear as <tag> and </tag>.
Rules
• Tags must be closed with the correct corresponding closing tag.
• Tags must be closed in the correct order.

Example Inputs
Input: "<html><body></body></html>"
Input: "<div><p></div></p>"
Input: "<a><b></b></a>"
Output: Valid
Output: Invalid
Output: Valid

tepid cargoBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

small scarab
#

How to compare the opening tag with closing tag?
Opening tags don't start with a slash
Closing tags start with a slash

upper peak
#

yeah?

small scarab
#

yeah

upper peak
#

so if dont have opening tags push if we have a slash.. then how to check if the tags are equavlent for each other

#

probably will check index 1 if it has slash or not ?

small scarab
#

Just look at it:

<a><b></b></a>
 1  2   3   4

1 = opening tag for a
2 = opening tag for b
3 = closing tag for b
4 = closing tag for a

upper peak
#

omm..

#

but

#

let's say we have <b> </a>

#

the tags are not appropriate

#

we need to check what is there

small scarab
#

well, you "also" need to store the name of the tag of course.

upper peak
#

should we check index[1] for slash?

#

to say whether we'll push or pop ?

small scarab
#

You only have push and pop (well, and peek)

upper peak
#

I mean

#

we'll be having the string like <a> <b> </b> </a>

#

so we can take its tokens

#

we will end up having

#

<a>
<b>
</b>
</a>

#

in an array

#

so if we used foreach

#

and looped through each element in that string array of tokens

#

we'll access the index of each tag

#

or maybe I am complicating it idk

small scarab
#

you just iterate over that array of tags, and for each opening tag you push that tag and for each closing tag you pop and check if the popped opening tag matches the closing tag

upper peak
#

okay hold up

#

and check if the opening tag matches the closing tag

#

how

#

there string are different

small scarab
#

and check if the popped opening tag matches the closing tag

upper peak
#

if we are comparing <a> to </a>

#

theyre different

small scarab
#

I edited that popped in afterwards

small scarab
upper peak
#

so u mean we'll just compare " a" to "a" not <a> to </a> ?

#

omm...

#

we need somehow to get rid of the < / > and store it in a new string

#

to do the comparison

small scarab
upper peak
#

interesting...

small scarab
#

But extracting it first, then storing the stripped version would also work

#

How you want to do it is up to you

upper peak
#

most efficient way

#

probably going to be the direct comparison

small scarab
#

Yeah, but since you're a beginner you shouldn't think about: What is the most efficient way, but: What is the easiest way.
If you think that the comparison adaption is easier, go for it (I personally think it is easier), if you think you'll have an easier time implementing the string extraction to make the comparison easier, then do that.

upper peak
#

I think the comparison way would be easy

#

but I have to figure out exactly how to apply it

#

lemme give it a thought

#

the token method

#

is correct right?

#

like string[] tokens=tags.split(">").trim();

#

oh I think I figured it out

tepid cargoBOT
#

@upper peak Has your question been resolved? If so, type !solved :)

upper peak
#

!solved