From cd29fc757e40bddad75d10788491114e272a7fb0 Mon Sep 17 00:00:00 2001 From: JadeyTuff <155403590+JadeyTuff@users.noreply.github.com> Date: Mon, 1 Jan 2024 18:07:52 -0800 Subject: [PATCH 1/3] Update bleep.cpp --- 8-references-and-pointers/bleep/bleep.cpp | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) 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"; + } From b9018e5b855d50366ff7939e36b6add01ef54e64 Mon Sep 17 00:00:00 2001 From: JadeyTuff <155403590+JadeyTuff@users.noreply.github.com> Date: Mon, 1 Jan 2024 18:09:13 -0800 Subject: [PATCH 2/3] Update functions.cpp simplifying functions using string methods --- 8-references-and-pointers/bleep/functions.cpp | 48 +++++++------------ 1 file changed, 17 insertions(+), 31 deletions(-) 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; + } From 4344988c906ae7c2e7115865cb28cd6f5f61de1b Mon Sep 17 00:00:00 2001 From: JadeyTuff <155403590+JadeyTuff@users.noreply.github.com> Date: Mon, 1 Jan 2024 18:09:38 -0800 Subject: [PATCH 3/3] Update functions.hpp --- 8-references-and-pointers/bleep/functions.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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);