Created
November 6, 2012 21:14
-
-
Save itsjohncs/4027602 to your computer and use it in GitHub Desktop.
Lab Session 6 - Answer to Exercise 4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
string extract_bolds(string input) { | |
string result; | |
string working = input; | |
while (working.find("<b>") != -1 && working.find("</b>") != -1) { | |
int first = working.find("<b>") + 3; | |
int second = working.find("</b>"); | |
string word = working.substr(first, second - first); | |
result += word + ", "; | |
working = working.substr(second + 4); | |
} | |
return result; | |
} | |
void test_extract_bolds(string sentence, string expected) { | |
string result = extract_bolds(sentence); | |
if (result != expected) { | |
cout << "FAILURE: Failed on input '" << sentence << "'. Expected '" | |
<< expected << "' but got '" << result << "'." << endl; | |
} else { | |
cout << "SUCCESS: Succeeded on input '" << sentence << "'. Got '" | |
<< result << "'." << endl; | |
} | |
} | |
int main() { | |
test_extract_bolds("Hello <b>world</b>!", "world, "); | |
test_extract_bolds("<b>Hello</b> cruel <b>world!</b>", "Hello, world!, "); | |
test_extract_bolds("To <b>succeed</b> in <b>life</b> one must be <b>bold</b>!", "succeed, life, bold, "); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment