diff --git a/8-references-and-pointers/bleep/bleep.cpp b/8-references-and-pointers/bleep/bleep.cpp index 9b9b816..2789ccd 100644 --- a/8-references-and-pointers/bleep/bleep.cpp +++ b/8-references-and-pointers/bleep/bleep.cpp @@ -1,22 +1,15 @@ #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"; - -} +using namespace std; + +int main(){ + + string word = "broccoli"; + string text = "I haven't had broccoli in a long time. I do miss broccoli sometimes. I should add broccoli to my groceries list. That's done. That way I'm sure to not forget to add broccoli to my basket, my broccoli basket, on my broccoli grocery trip. #broccoli"; + + bleep(word, text); + cout << text; + + return 0; +} \ No newline at end of file diff --git a/8-references-and-pointers/bleep/functions.cpp b/8-references-and-pointers/bleep/functions.cpp index 1a63591..dd2b3b1 100644 --- a/8-references-and-pointers/bleep/functions.cpp +++ b/8-references-and-pointers/bleep/functions.cpp @@ -1,37 +1,21 @@ -#include +#include +using namespace std; -void asterisk(std::string word, std::string &text, int i) { - - for (int k = 0; k < word.size(); ++k) { - +void asterisk(string &word, string &text, int i){ + for(int k=0; k < word.length(); 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); - +string bleep(string word,string &text){ + + int word_len = word.length(); + int text_len = text.length(); + for (int i=0; i < (text_len-(word_len-1)); i++){ + string selected_seq = text.substr(i, word_len); + if (selected_seq == word){ + asterisk(selected_seq, text, i); } - } - -} + return text; +} \ No newline at end of file diff --git a/8-references-and-pointers/bleep/functions.hpp b/8-references-and-pointers/bleep/functions.hpp index f1fec96..e60324f 100644 --- a/8-references-and-pointers/bleep/functions.hpp +++ b/8-references-and-pointers/bleep/functions.hpp @@ -1,2 +1,4 @@ -void bleep(std::string word, std::string &text); -void asterisk(std::string word, std::string &text, int i); +using namespace std; + +string bleep(string word,string &text); +void asterisk(string &word, string &text, int i); \ No newline at end of file diff --git a/bleep.cpp b/bleep.cpp new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/bleep.cpp @@ -0,0 +1 @@ +