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

Slightly different Bleep approach #80

Open
wants to merge 1 commit 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
31 changes: 12 additions & 19 deletions 8-references-and-pointers/bleep/bleep.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
#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";

}
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;
}
44 changes: 14 additions & 30 deletions 8-references-and-pointers/bleep/functions.cpp
Original file line number Diff line number Diff line change
@@ -1,37 +1,21 @@
#include <string>
#include <iostream>
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;
}
6 changes: 4 additions & 2 deletions 8-references-and-pointers/bleep/functions.hpp
Original file line number Diff line number Diff line change
@@ -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);
1 change: 1 addition & 0 deletions bleep.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@