what is the use of ‘int i’ and ‘&’ in the code?

  c++

The following C++ code takes two strings. string 1 is the text which is assigned to I hate broccoli. and the other string is the word which is assigned to broccoli. The goal of the program tells: wherever the word is present in text, replace it by +. so in this case, the output is I hate ********

what is the use of int i in void asterisk(std::string word, std::string &text, int i) and what does & do in this program?

code.cpp

#include <iostream>
#include <string>
#include "code.hpp"

int main() {
  
  std::string word = "broccoli";
  
  std::string text = "I hate broccoli.";
  
  bleep(word, text);
  
  for (int i = 0; i < text.size(); i++) {
    
    std::cout << text[i];
    
  }
  
  std::cout << "n";
  
}


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

code.hpp

void asterisk(std::string word, std::string &text, int i);
void bleep(std::string word, std::string &text);

Source: Windows Questions C++

LEAVE A COMMENT