diff --git a/8-references-and-pointers/bleep/bleep.cpp b/8-references-and-pointers/bleep/bleep.cpp index 9b9b816..7680ec4 100644 --- a/8-references-and-pointers/bleep/bleep.cpp +++ b/8-references-and-pointers/bleep/bleep.cpp @@ -3,20 +3,20 @@ #include "functions.hpp" +using namespace std; + 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"; - + // text that will be edited + string text = "I sometimes eat broccoli. There are three 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"; + // word that I will replace + string word = "broccoli"; + // bleep function call + bleep(text, word); + + // print edited text + std::cout << text << "\n"; + + return 0; + } diff --git a/8-references-and-pointers/bleep/functions.cpp b/8-references-and-pointers/bleep/functions.cpp index 1a63591..2bce0f0 100644 --- a/8-references-and-pointers/bleep/functions.cpp +++ b/8-references-and-pointers/bleep/functions.cpp @@ -1,37 +1,23 @@ #include -void asterisk(std::string word, std::string &text, int i) { - - for (int k = 0; k < word.size(); ++k) { - - text[i+k] = '*'; - - } - -} +using namespace std; -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) { +void bleep(string &text, string word) { + + // main loop that will iterate through all the text + for (int i = 0; i < text.length(); i++) { - if (text[i+j] == word[j]) { - - ++match; - - } + //uses sub string to check if the word broccoli shows up + string check = text.substr(i, word.length()); - } + //sees if word is broccoli and replaces it with * + if (check == word) { + + for (int n = 0; n < word.length(); n++) { - if (match == word.size()) { - - asterisk(word, text, i); + text[i + n] = '*'; + } } - } - } diff --git a/8-references-and-pointers/bleep/functions.hpp b/8-references-and-pointers/bleep/functions.hpp index f1fec96..4778cd2 100644 --- a/8-references-and-pointers/bleep/functions.hpp +++ b/8-references-and-pointers/bleep/functions.hpp @@ -1,2 +1,2 @@ -void bleep(std::string word, std::string &text); -void asterisk(std::string word, std::string &text, int i); +// bleep function declaration +void bleep(std::string &text, std::string word);