diff --git a/exercises/acronym/example.cpp b/exercises/acronym/example.cpp index 0f7c607e..a92e40d4 100644 --- a/exercises/acronym/example.cpp +++ b/exercises/acronym/example.cpp @@ -1,23 +1,24 @@ -#include -#include +#include #include "acronym.h" using std::string; -using std::vector; namespace acronym { -string acronym(string const& input_str) -{ - vector 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 diff --git a/exercises/anagram/example.cpp b/exercises/anagram/example.cpp index dd7aa79e..ee753a20 100644 --- a/exercises/anagram/example.cpp +++ b/exercises/anagram/example.cpp @@ -1,5 +1,4 @@ #include "anagram.h" -#include #include #include @@ -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; } @@ -31,7 +39,7 @@ vector anagram::matches(vector const& matches) vector 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); } diff --git a/exercises/bob/example.cpp b/exercises/bob/example.cpp index 293bf764..793f3e71 100644 --- a/exercises/bob/example.cpp +++ b/exercises/bob/example.cpp @@ -1,5 +1,4 @@ #include "bob.h" -#include #include #include #include @@ -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); @@ -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; } } diff --git a/exercises/crypto-square/example.cpp b/exercises/crypto-square/example.cpp index 78474a9c..4cf16e0b 100644 --- a/exercises/crypto-square/example.cpp +++ b/exercises/crypto-square/example.cpp @@ -1,5 +1,4 @@ #include "crypto_square.h" -#include #include #include #include @@ -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); } } diff --git a/exercises/word-count/example.cpp b/exercises/word-count/example.cpp index 1e9b762c..6753df34 100644 --- a/exercises/word-count/example.cpp +++ b/exercises/word-count/example.cpp @@ -3,13 +3,42 @@ #include #include #include -#include 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 split(string const& s, string const& delims) +{ + auto start = s.begin(); + vector 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; @@ -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 split_text_into_words(string const& text) { - vector words; - boost::split(words, text, boost::is_any_of("\t ")); + vector words = split(text, "\t "); transform(words.begin(), words.end(), words.begin(), trim_word); return words; }