Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to under the logic behind these two C++ functions especially the part where

if (text[i+j] == word[j])

text[k+i] = '*'

these two functions are created to change broccoli to asterisk * in the sentence.Thank you so much in advance.

C++
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.";

<pre>#include <string>

void asterisk(std::string word, std::string &text, int i) {
  
  for (int k = 0; k < word.size(); ++k) {
  
    text[k+i] = '*';      
  }  
}

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);        
    }      
  }   
}


What I have tried:

there is nothing wrong with the code.
Posted
Updated 14-Jul-19 22:14pm
v2
Comments
KarstenK 15-Jul-19 4:15am    
This aint "magic" but some operators which do their job ;-)

Quote:
I am trying to under the logic behind these two C++ functions

When you don't understand what does some code or how it does, use the debugger and watch it perform.

Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.

With this change, the code will be 8 times faster.
C++
void bleep(std::string word, std::string &text) {
    for (int i = 0; i < text.size(); ++i) {
        if (text[i] == word[0]) {
            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);
            }
        }
    }
}
 
Share this answer
 
v2
The code is comparing the strings word and text, character by character. There is a nested for loop that for every character in text (indexed by i), it checks all of word (indexed by j) and counts the number of characters that match. When it finds a matching sequence of the same size as word then it replaces the matching characters in text with asterisks.

Remember that text is a string, or an array of characters. The expression text[index] accesses the character in the string at location index. Index can be any valid expression like 4 or i+j or whatever-42.
 
Share this answer
 
v2
Comments
millionairejason 14-Jul-19 23:45pm    
That makes so much sense to me now , thank you .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900