How to resolve the algorithm Count occurrences of a substring step by step in the C++ programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Count occurrences of a substring step by step in the C++ programming language

Table of Contents

Problem Statement

Create a function,   or show a built-in function,   to count the number of non-overlapping occurrences of a substring inside a string. The function should take two arguments:

It should return an integer count. The matching should yield the highest number of non-overlapping matches. In general, this essentially means matching from left-to-right or right-to-left   (see proof on talk page).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Count occurrences of a substring step by step in the C++ programming language

The provided code counts the number of non-overlapping occurrences of a substring within a given string. A brief overview of how it works:

Function:

  • The countSubstring function takes two arguments: str (the main string) and sub (the substring to search for).

Counting Loop:

  • The function initializes a counter variable count to 0.
  • It uses a for loop to iterate through the str string, starting from the beginning.
  • In each iteration, it uses the find method to find the first occurrence of sub in str, starting from the current offset offset.
  • If sub is found at the current offset, it increments count.
  • The loop continues until there are no more occurrences of sub in str (i.e., find returns std::string::npos).

Special Case:

  • If sub is an empty string, the function returns 0 because there can be no occurrences of an empty substring.

Main Function:

  • In the main function, the countSubstring function is called three times with different input strings (str and sub pairs).
  • The results are printed to the console.

Example Input and Output:

  • Input: countSubstring("the three truths", "th")
    • Output: 2 (two occurrences of "th")
  • Input: countSubstring("ababababab", "abab")
    • Output: 3 (three occurrences of "abab")
  • Input: countSubstring("abaabba*bbaba*bbab", "a*b")
    • Output: 4 (four occurrences of "a*b")

Source code in the cpp programming language

#include <iostream>
#include <string>

// returns count of non-overlapping occurrences of 'sub' in 'str'
int countSubstring(const std::string& str, const std::string& sub)
{
    if (sub.length() == 0) return 0;
    int count = 0;
    for (size_t offset = str.find(sub); offset != std::string::npos;
	 offset = str.find(sub, offset + sub.length()))
    {
        ++count;
    }
    return count;
}

int main()
{
    std::cout << countSubstring("the three truths", "th")    << '\n';
    std::cout << countSubstring("ababababab", "abab")        << '\n';
    std::cout << countSubstring("abaabba*bbaba*bbab", "a*b") << '\n';

    return 0;
}


  

You may also check:How to resolve the algorithm Left factorials step by step in the Fōrmulæ programming language
You may also check:How to resolve the algorithm Josephus problem step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Dot product step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Averages/Median step by step in the Python programming language
You may also check:How to resolve the algorithm Sort an array of composite structures step by step in the Ol programming language