Write a program that receives XHTML as input and outputs the number of XHTML tags in the string. The program should count the number of tags nested at each level. For example, the XHTML:

hi

has a p tag (nesting level 0—i.e., not nested in another tag) and a strong tag (nesting level 1).

What will be an ideal response?


```
# Count the total HTML tags and the number of tags nested
# at each level in a string.

hString = raw_input( "Please enter an XHTML string: " )

levels = ( hString.count( "<" ) ) / 2

tags = []

for level in range( 0, levels ):
tags.append( 0 )

level = -1
start = 0 # begin with index 0

while 1:

try:
start = hString.index( "<", start ) + 1

if hString[ start ] == "/": # closing tag
level -= 1 # decrease level
else: # else opening tag
level += 1

# increment nesting count
for i in range( 0, level ):
tags[ i ] += 1

except ValueError: # no more tags in string
break

for level in range( 0, levels ):
print "At level %d, number of nested tags: %d." % \
( level, tags[ level ] )
```
Please enter an XHTML string: My Webpage

At level 0, number of nested tags: 2.
At level 1, number of nested tags: 1.
At level 2, number of nested tags: 0.

Computer Science & Information Technology

You might also like to view...

________ allow you to perform actions like zooming and switching programs by performing certain movements

A) Gestures B) Clicks C) Icons D) Swings

Computer Science & Information Technology

A hyperlinked custom slide show enables the presenter to quickly navigate to another presentation from within the current presentation

Indicate whether the statement is true or false

Computer Science & Information Technology

The cost of accounting software for a small business can be several hundred thousand dollars.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

In an active topology, each node helps to move data through a network.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology