Target Search Program

This is a little program that I made as apart of project on Codecademy. The logic and search process is super simple. I am still pretty proud as i'm barely learning c++. I was just wondering how I could make the search much more efficient or even the program as a whole. This is the code I wrote. bool contains_target(const std::string& word, const std::string& target){ int word_length = word.length(); int target_length = target.length(); if(target_length > word_length) { return false; } for(int i = 0; i

May 5, 2025 - 18:20
 0
Target Search Program

This is a little program that I made as apart of project on Codecademy. The logic and search process is super simple. I am still pretty proud as i'm barely learning c++. I was just wondering how I could make the search much more efficient or even the program as a whole.

This is the code I wrote.

bool contains_target(const std::string& word, const std::string& target){
  int word_length = word.length();
  int target_length = target.length();

  if(target_length > word_length) {
    return false;
  }

  for(int i = 0; i <= word_length - target_length; i++){
    if(word[i] == target[0]){
      std::string sub = word.substr(i, target_length);
      if(sub == target){
        return true;
      }
    }
  }
  return false;
}