Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions exercises/acronym/example.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
#include <vector>
#include <boost/algorithm/string.hpp>
#include <cctype>
#include "acronym.h"

using std::string;
using std::vector;

namespace acronym {

string acronym(string const& input_str)
{
vector<string> words;
boost::split(words, input_str, boost::is_any_of(" :-,"));
string acronym(string const &input_str) {
string acronym_str;
for (auto const& s : words) {
if (!s.empty()) {
acronym_str.push_back((boost::algorithm::to_upper_copy(s)).front());
string delimiters = " :-,";
enum DelimiterState { NOT_FOUND, FOUND };
DelimiterState delimiter_state = FOUND;
for (char c : input_str) {
if (delimiters.find(c) != string::npos) {
delimiter_state = FOUND;
} else if (delimiter_state == FOUND) {
delimiter_state = NOT_FOUND;
acronym_str.push_back(std::toupper(c));
}
}
return acronym_str;
}

}
} // namespace acronym
14 changes: 11 additions & 3 deletions exercises/anagram/example.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "anagram.h"
#include <boost/algorithm/string/case_conv.hpp>
#include <algorithm>
#include <cctype>

Expand All @@ -8,9 +7,18 @@ using namespace std;
namespace
{

string to_lower_copy(std::string const& s)
{
string cpy(s);
for (auto& c: cpy) {
c = tolower(c);
}
return cpy;
}

string make_key(std::string const& s)
{
string key{boost::to_lower_copy(s)};
string key{to_lower_copy(s)};
sort(key.begin(), key.end());
return key;
}
Expand All @@ -31,7 +39,7 @@ vector<string> anagram::matches(vector<string> const& matches)
vector<string> result;
for (string const& s : matches) {
if (s.length() == key_.length()
&& boost::to_lower_copy(s) != boost::to_lower_copy(subject_)
&& to_lower_copy(s) != to_lower_copy(subject_)
&& make_key(s) == key_) {
result.push_back(s);
}
Expand Down
17 changes: 14 additions & 3 deletions exercises/bob/example.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "bob.h"
#include <boost/algorithm/string/trim.hpp>
#include <algorithm>
#include <cctype>
#include <iterator>
Expand All @@ -11,6 +10,18 @@ namespace bob
namespace
{

string trim_copy(string const& s)
{
string cpy(s);
// Trim front
while (!cpy.empty() && cpy.front() == ' ')
cpy.erase(cpy.begin());
// Trim back
while (!cpy.empty() && cpy.back() == ' ')
cpy.pop_back();
return cpy;
}

bool is_upper(string const& text)
{
auto last = end(text);
Expand All @@ -33,12 +44,12 @@ bool is_shouting(string const &text)

bool is_question(string const &text)
{
return boost::algorithm::trim_copy(text).back() == '?';
return trim_copy(text).back() == '?';
}

bool is_silence(string const& text)
{
return boost::algorithm::trim_copy(text).length() == 0;
return trim_copy(text).length() == 0;
}

}
Expand Down
12 changes: 10 additions & 2 deletions exercises/crypto-square/example.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "crypto_square.h"
#include <boost/algorithm/string/case_conv.hpp>
#include <algorithm>
#include <cctype>
#include <iterator>
Expand All @@ -9,12 +8,21 @@ using namespace std;
namespace
{

string to_lower_copy(std::string& s)
{
string cpy(s);
for (auto& c: cpy) {
c = tolower(c);
}
return cpy;
}

string normalize(string const &text)
{
string result;
copy_if(text.begin(), text.end(), back_inserter(result),
[](const char c) { return !isspace(c) && !ispunct(c); });
return boost::to_lower_copy(result);
return to_lower_copy(result);
}

}
Expand Down
36 changes: 32 additions & 4 deletions exercises/word-count/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,42 @@
#include <cctype>
#include <iterator>
#include <vector>
#include <boost/algorithm/string.hpp>

using namespace std;

namespace
{

string trim_copy_if(string const& s, string const& delims)
{
string cpy(s);
// Trim front
while (!cpy.empty() && delims.find(cpy.front()) != string::npos)
cpy.erase(cpy.begin());
// Trim back
while (!cpy.empty() && delims.find(cpy.back()) != string::npos)
cpy.pop_back();
return cpy;
}

vector<string> split(string const& s, string const& delims)
{
auto start = s.begin();
vector<string> words;
for (auto end = s.begin(); end != s.end(); end++) {
if (delims.find(*end) != string::npos) {
if (start != end) {
words.push_back(s.substr(start - s.begin(), end - start));
}
start = end + 1;
}
}
if (start < s.end()) {
words.push_back(s.substr(start - s.begin(), s.end() - start));
}
return words;
}

string normalize_text(string const& text)
{
string normalized;
Expand All @@ -20,13 +49,12 @@ string normalize_text(string const& text)

string trim_word(string const& word)
{
return boost::trim_copy_if(word, boost::is_any_of("' "));
return trim_copy_if(word, "' ");
}

vector<string> split_text_into_words(string const& text)
{
vector<string> words;
boost::split(words, text, boost::is_any_of("\t "));
vector<string> words = split(text, "\t ");
transform(words.begin(), words.end(), words.begin(), trim_word);
return words;
}
Expand Down