// Copyright 2026 Amber Hu
#include <cstdlib>   // EXIT_SUCCESS
#include <string>
#include <iostream>

using std::string;

/// If `search` is substring within `input`, output the substring
/// of input from [0,n) into result, where n is the index
/// of the first char in the search substring within input.
/// Returns the length of result.
size_t StringBefore(const string& input, const string& search, 
                  string* const result) {
  *result = input.substr(0, input.find(search));
  return input.find(search);
}

int main(int argc, char** argv) {

  const string full("mydir.tar.gz");
  std::cout << "full: " << full << std::endl;
  const string search(".tar");
  std::cout << "search: " << search << std::endl;

  string substring;
  size_t index = StringBefore(full, search, &substring);

  std::cout << "Length " << index << ", result: " << substring << std::endl;
  return EXIT_SUCCESS;
}