From 35cc3230f236497b5c7d39a5b1d366ff8ac9c233 Mon Sep 17 00:00:00 2001 From: lethargy-reigns-supreme <82919344+lethargy-reigns-supreme@users.noreply.github.com> Date: Mon, 27 Jun 2022 12:57:44 -0500 Subject: [PATCH 1/3] different logic that uses less loops --- 8-references-and-pointers/bleep/bleep.cpp | 28 +++++++++++------------ 1 file changed, 14 insertions(+), 14 deletions(-) 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; + } From 92767e6e638fb76f759d9d4655e3c0b28965edf7 Mon Sep 17 00:00:00 2001 From: lethargy-reigns-supreme <82919344+lethargy-reigns-supreme@users.noreply.github.com> Date: Mon, 27 Jun 2022 13:03:07 -0500 Subject: [PATCH 2/3] different logic using substr --- 8-references-and-pointers/bleep/functions.cpp | 40 ++++++------------- 1 file changed, 13 insertions(+), 27 deletions(-) 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] = '*'; + } } - } - } From adfbd295e02f3ac8245b649a2bf5d232016ba83f Mon Sep 17 00:00:00 2001 From: lethargy-reigns-supreme <82919344+lethargy-reigns-supreme@users.noreply.github.com> Date: Mon, 27 Jun 2022 13:04:22 -0500 Subject: [PATCH 3/3] Update functions.hpp --- 8-references-and-pointers/bleep/functions.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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);