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

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

View Full Document Right Arrow Icon
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 & : 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
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/20/2010 for the course CS 443 taught by Professor Jayana during the Spring '10 term at École Normale Supérieure.

Page1 / 3

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

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online