How to count the number of non-overlapping instances of a substring in a string in C++

4 Answers

0 votes
#include <iostream>
#include <string>
  
int countOccurrences(const std::string str, const std::string substr) {
    if (substr.empty()) return 0;
      
    int count = 0;
    for (size_t offset = str.find(substr); 
         offset != std::string::npos; 
         offset = str.find(substr, offset + substr.length())) {
            count++;
    }
      
    return count;
}
  
int main() {
    std::string s = "go java phphp rust c pphpp c++ phpphp python php phphp";
      
    std::cout << countOccurrences(s, "php") << std::endl;
}
  
  
/*
run:
  
6
  
*/
 

 



answered Aug 24, 2024 by avibootz
edited 5 hours ago by avibootz
0 votes
#include <iostream>
#include <string>
#include <regex>
 
int countNonOverlappingOccurrences(const std::string str, const std::string substr) {
    std::regex re(substr, std::regex_constants::icase);
    std::string replaced = std::regex_replace(str, re, "");
 
    return (str.length() - replaced.length()) / substr.length();
}
 
int main() {
    std::string s = "go java phphp rust c pphpp c++ phpphp python php phphp";
     
    std::cout << countNonOverlappingOccurrences(s, "php") << std::endl;
}
 
  
  
/*
run:
  
6
  
*/

 



answered Aug 25, 2024 by avibootz
edited 5 hours ago by avibootz
0 votes
#include <iostream>
#include <string>
#include <regex>

// Non‑overlapping occurrences are matches of a substring that do not reuse any of
// the same characters. Once one match is counted, the next search must begin
// after that match ends.

int count(const std::string& s, const std::string& substr) {
    /*
       Count how many times 'substr' appears in 's'.
       This version uses std::regex + std::sregex_iterator.
       Note: std::sregex_iterator counts overlapping matches,
       just like Scala's Regex.findAllIn.
    */

    std::regex pattern(substr);
    auto begin = std::sregex_iterator(s.begin(), s.end(), pattern);
    auto end   = std::sregex_iterator();

    return std::distance(begin, end);
}

int main() {
    // Demonstration using the string provided in the instructions:
    std::string s = "go java phphp rust c pphpp c++ phpphp python php phphp";

    // Count occurrences
    std::cout << count(s, "php") << std::endl;

    return 0;
}


/*
run:

6

*/

 



answered 5 hours ago by avibootz
0 votes
#include <iostream>
#include <string>

// Non‑overlapping occurrences are matches of a substring that do not reuse any of
// the same characters. Once one match is counted, the next search must begin
// after that match ends.

int count_non_overlapping(const std::string& haystack, const std::string& needle) {
    /*
       Count how many times 'needle' appears in 'haystack' without overlapping.
       The algorithm:
         • Use std::string::find() to locate the next occurrence.
         • Each time a match is found, move the search index forward
           by the full length of the matched substring.
         • This ensures no characters are reused between matches.
    */

    int count = 0;
    std::size_t index = 0;  // current search position in the main string

    // Continue searching until .find() returns std::string::npos (meaning: no more matches)
    while (true) {
        // Find the next occurrence starting at the current index
        std::size_t pos = haystack.find(needle, index);

        if (pos == std::string::npos) {
            // No more matches found
            break;
        }

        // We found a match, so increment the count
        count++;

        // Move index forward by the length of the needle
        // This ensures the next search begins *after* the matched substring
        index = pos + needle.size();
    }

    return count;
}


// ---------------------------------------------------------------
int main() {
    // Demonstration using the string provided in the instructions:
    std::string s = "go java phphp rust c pphpp c++ phpphp python php phphp";
    std::string substring = "php";

    // Count non-overlapping occurrences
    int result = count_non_overlapping(s, substring);

    std::cout << "Non-overlapping occurrences: " << result << std::endl;
}



/*
run:

Non-overlapping occurrences: 6

*/

 



answered 5 hours ago by avibootz

Related questions

...