Skip to content

Instantly share code, notes, and snippets.

@DanielDe
Created November 8, 2012 18:50
Show Gist options
  • Save DanielDe/4040724 to your computer and use it in GitHub Desktop.
Save DanielDe/4040724 to your computer and use it in GitHub Desktop.
CS 10 SI Week 6 Lab Exercise Solutions
#include <iostream>
using namespace std;
string extract_bold(string);
int main() {
string test1 = "To <b>succeed</b> in <b>life</b> one must be <b>bold</b>!";
cout << extract_bold(test1) << endl;
return 0;
}
string extract_bold(string htmlText) {
// these tags and their lengths will never change, so we'll make them const
const string OPEN_TAG = "<b>";
const int OPEN_LENGTH = OPEN_TAG.size();
const string CLOSE_TAG = "</b>";
const int CLOSE_LENGTH = CLOSE_TAG.size();
// this string will hold our comma separated list of results
string results = "";
// this is where we'll start searching for the next <b> tag
int findStartIndex = 0;
// we'll keep searching so long as there are more <b> tags in the string
while (htmlText.find(OPEN_TAG, findStartIndex) != -1) {
// find the beginning and end of our next result
int beginIndex = htmlText.find(OPEN_TAG, findStartIndex) + OPEN_LENGTH;
int endIndex = htmlText.find(CLOSE_TAG, findStartIndex);
// substr expects a length, so we'll calculate the length of our next result
int length = endIndex - beginIndex;
// extract the next result using substring
string nextResult = htmlText.substr(beginIndex, length);
// append this next result (and a comma) to our results string
results = results + ", " + nextResult;
// our new starting search index is after the </b> tag
findStartIndex = endIndex + CLOSE_LENGTH;
}
// if there are any results, they will begin with ", " so we'll eliminate those
if (results.size() > 0)
results = results.substr(2);
// return the answer
return results;
}
#include <iostream>
using namespace std;
string extract_bold(string);
int main() {
string test1 = "To <b>succeed</b> in <b>life</b> one must be <b>bold</b>!";
cout << extract_bold(test1) << endl;
return 0;
}
string extract_bold(string htmlText) {
// these tags and their lengths will never change, so we'll make them const
const string OPEN_TAG = "<b>";
const int OPEN_LENGTH = OPEN_TAG.size();
const string CLOSE_TAG = "</b>";
const int CLOSE_LENGTH = CLOSE_TAG.size();
// it is bad practice to modify parameters, so we'll use a new variable instead
string remainder = htmlText;
// this string will hold our comma separated list of results
string results = "";
// we'll keep searching so long as there are more <b> tags in the string
while (remainder.find(OPEN_TAG) != -1) {
// find the beginning and end of our next result
int beginIndex = remainder.find(OPEN_TAG) + OPEN_LENGTH;
int endIndex = remainder.find(CLOSE_TAG);
// substr expects a length, so we'll calculate the length of our next result
int length = endIndex - beginIndex;
// extract the next result using substring
string nextResult = remainder.substr(beginIndex, length);
// append this next result (and a comma) to our results string
results = results + ", " + nextResult;
// the remaining string we have to search is everything after the </b> tag
remainder = remainder.substr(endIndex + CLOSE_LENGTH);
}
// if there are any results, they will begin with ", " so we'll eliminate those
if (results.size() > 0)
results = results.substr(2);
// return the answer
return results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment