Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 5 additions & 15 deletions 8-references-and-pointers/bleep/bleep.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
#include <iostream>
#include <string>

#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";

}
48 changes: 17 additions & 31 deletions 8-references-and-pointers/bleep/functions.cpp
Original file line number Diff line number Diff line change
@@ -1,37 +1,23 @@
#include <string>
#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;

}
3 changes: 1 addition & 2 deletions 8-references-and-pointers/bleep/functions.hpp
Original file line number Diff line number Diff line change
@@ -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);