Related questions with answers
We assume that opening tags in HTML have form HTML stands for __________. Solution HTML stands for . 1/4 1/7. Modify Code Fragment so that it can properly match tags, even when an opening tag may include one or more such attributes.
1 /** Tests if every opening tag has a matching closing tag in HTML string. */
2 public static boolean isHTMLMatched(String html) {
3 Stack<String> buffer = new LinkedStack<>( );
4 int j = html.indexOf('<'); // find first ’<’ character (if any)
5 while (j != −1) {
6 int k = html.indexOf('>', j+1); // find next ’>’ character
7 if (k == −1)
8 return false; // invalid tag
9 String tag = html.substring(j+1, k); // strip away < >
10 if (!tag.startsWith("/")) // this is an opening tag
11 buffer.push(tag);
12 else { // this is a closing tag
13 if (buffer.isEmpty( ))
14 return false; // no tag to match
15 if (!tag.substring(1).equals(buffer.pop( )))
16 return false; // mismatched tag
17 }
18 j = html.indexOf('<', k+1); // find next ’<’ character (if any)
19 }
20 return buffer.isEmpty( ); // were all opening tags matched?
21 }
Create a free account to view solutions
Create a free account to view solutions
Recommended textbook solutions
Java Software Solutions for AP Computer Science
3rd Edition•ISBN: 9780131374690Cara Cocking, John Lewis, William LoftusIntroduction to Java Programming and Data Structures, Comprehensive Version
11th Edition•ISBN: 9780134700144 (1 more)Y. Daniel LiangFundamentals of Java: AP Computer Science Essentials
4th Edition•ISBN: 9780538744928Kenneth Lambert, Martin OsborneJava Methods: Object-Oriented Programming and Data Structures
3rd Edition•ISBN: 9780982477564Gary Litvin, Maria LitvinMore related questions