diff --git a/8-references-and-pointers/bleep/bleep.cpp b/8-references-and-pointers/bleep/bleep.cpp index 9b9b816..9f1277b 100644 --- a/8-references-and-pointers/bleep/bleep.cpp +++ b/8-references-and-pointers/bleep/bleep.cpp @@ -1,22 +1,12 @@ #include #include - #include "functions.hpp" int main() { - + std::string word = "broccoli"; - - std::string sentence = "I sometimes eat broccoli. The interesting thing about broccoli is that there are four interesting things about broccoli. Number One. Nobody knows how to spell it. Number Two. No matter how long you boil it, it's always cold by the time it reaches your plate. Number Three. It's green. #broccoli"; - - bleep(word, sentence); - - for (int i = 0; i < sentence.size(); i++) { - - std::cout << sentence[i]; - - } - - std::cout << "\n"; - + std::string text = "Barclay sounds like broccoli and calling someone broccoli is funny."; + + std::cout << bleep(word, text) << "\n"; + } diff --git a/8-references-and-pointers/bleep/functions.cpp b/8-references-and-pointers/bleep/functions.cpp index 1a63591..f4c1d88 100644 --- a/8-references-and-pointers/bleep/functions.cpp +++ b/8-references-and-pointers/bleep/functions.cpp @@ -1,37 +1,23 @@ #include +#include "functions.hpp" + +std::string bleep(std::string &bword, std::string &btext) { + + std::string stars = "*"; + + while (stars.length() < bword.length()) { + + stars += "*"; -void asterisk(std::string word, std::string &text, int i) { - - for (int k = 0; k < word.size(); ++k) { - - text[i+k] = '*'; - } - -} -void bleep(std::string word, std::string &text) { - - for (int i = 0; i < text.size(); ++i) { - - int match = 0; - - for (int j = 0; j < word.size(); ++j) { - - if (text[i+j] == word[j]) { - - ++match; - - } - - } - - if (match == word.size()) { - - asterisk(word, text, i); - - } - + //loop through replacing "broccoli" until .find is greater than the length of btext + while (btext.find(bword) < btext.length()) { + + btext = btext.replace(btext.find(bword), bword.length(), stars); + } - + + return btext; + } diff --git a/8-references-and-pointers/bleep/functions.hpp b/8-references-and-pointers/bleep/functions.hpp index f1fec96..7006c50 100644 --- a/8-references-and-pointers/bleep/functions.hpp +++ b/8-references-and-pointers/bleep/functions.hpp @@ -1,2 +1 @@ -void bleep(std::string word, std::string &text); -void asterisk(std::string word, std::string &text, int i); +std::string bleep(std::string &bword, std::string &btext);