{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

25S-CS106B-Solution

# 25S-CS106B-Solution - CS106B Spring 2010 Handout 25S May...

This preview shows pages 1–2. Sign up to view the full content.

CS106B Handout 25S Spring 2010 May 5 th , 2010 CS106B Midterm Solution Solution 1: HTML Entitles string RestoreString(string str, Map<char>& entityMap) { string translation; translation.reserve(str.size()); // optional, preallocates space // cpos stands for 'c'haracter 'pos'ition for (int cpos = 0; cpos < str.size(); cpos++) { if (str[cpos] == '&') { int scpos = str.find(';', cpos + 1); string entityCore = str.substr(cpos + 1, scpos - cpos - 1); if (entityMap.containsKey(entityCore)) { translation += entityMap[entityCore]; } else { translation += '&' + entityCore + ';'; } cpos = scpos; } else { translation += str[cpos]; } } return translation; } Criteria for Problem 1: 10 points Using either a character-oriented for loop, a Scanner , or a while loop that keeps calling string::find , processes each and every character in the supplied string: 1 point Appends a non- '&' character, as appropriate, when it’s clear we’re not in the middle of an HTML entity: 1 point Manages to find the index of the first semicolon that appears after a '&' : 1 point Properly extracts the core of the HTML entity—i.e. the amp of &amp; : 2 points Replaces the full HTML entity with the corresponding character, when, of course, the entity is in the map: 1 point Leaves the substring that looks like an HTML entity in the translation when it doesn’t appear in the map: 1 point Properly advances past the HTML entity (I have cpos = scpos , but they might have something else): 1 point

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}