Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using substr instead of a match counter #56

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
28 changes: 14 additions & 14 deletions 8-references-and-pointers/bleep/bleep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}
40 changes: 13 additions & 27 deletions 8-references-and-pointers/bleep/functions.cpp
Original file line number Diff line number Diff line change
@@ -1,37 +1,23 @@
#include <string>

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] = '*';

}
}

}

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