how to know the number of a certain substring in a string in c++

#include <iostream>
#include <string>

int countSubstringOccurrences(const std::string& mainStr, const std::string& subStr) {
    if (subStr.length() == 0) {
        return 0;
    }

    int count = 0;
    size_t pos = mainStr.find(subStr);

    while (pos != std::string::npos) {
        count++;
        pos = mainStr.find(subStr, pos + subStr.length());
    }

    return count;
}

int main() {
    std::string mainString, subString;

    // Input the main string
    std::cout << "Enter the main string: ";
    std::getline(std::cin, mainString);

    // Input the substring to count
    std::cout << "Enter the substring to count: ";
    std::getline(std::cin, subString);

    // Count and display the number of occurrences
    int occurrences = countSubstringOccurrences(mainString, subString);
    std::cout << "Number of occurrences: " << occurrences << std::endl;

    return 0;
}