We often need to count the number of times a substring can be found within a string. This problem is quite different from only checking whether a substring is found within a string, as this problem requires us to count the occurrences of a particular substring within a string and return that integer value. This value can be 0, 1, or more.
The find()
method can be very useful in this case.
find()
methodThe find()
method is used in C++ for finding the exact position of a substring within a string. Its syntax is as follows:
Str.find(Substr, Startpos)
Str
: This is the bigger string, in which we’ll look for the substring.
Substr
: This is the substring, which we are looking for within the Str
.
Startpos
: This is the position, from where our code will start finding the substring, within the bigger string. It is an optional argument, and by default it is 0; if not specified, it will search the substring from the start of the string.
The command above returns the position at which the substring occurs in the bigger string for the first time, ahead of the Startpos
. If the substring is not found, it will return the std::string::npos
, which is going to be much larger than the length of any imaginable string.
Run the following code for better understanding of how the find()
method works.
#include <iostream>#include <string>using namespace std;int main() {string str, substr;str = "no but no";substr = "no";cout << "Main string is: " << str << endl;cout << "The substring is: " << substr << "\n\n";cout << "The line of code 'str.find(substr)' searches for substring from the start, position 0, and returns: " << str.find(substr) << "\n";cout << "The line of code 'str.find(substr, 1)' starts searching for substring from position '1', and returns: " << str.find(substr, 1) << "\n\n";cout << "As 'a' isn't present in the string, the code 'str.find('a')' will return the npos of string, which is: " << str.find('a');}
Although find()
seems to be quite useful, it alone doesn't solve the problem at hand. We can, however, use it in our code in such a way that it helps us find each occurrence of the substring within the string.
The following code shows the solution to our problem:
#include <iostream>#include <string>using namespace std;int main() {string str, substr;str = "no but no but nonono,onon, no"; //Main stringsubstr = "no"; // Substringcout << "Main string is: " << str << endl;cout << "The substring is: " << substr << endl;cout << "\n\n";int total = 0; // This will be the total number of times the substring comes within the string/* The following code runs a while loop, which has the condition that the position returnedby .find() is smaller than length of string. If the next line doesn't find any occurrenceof the substring within the string, the while loop will not start. If it does, the whileloop will start, but will end when all the occurrences have been found.*/int pos = str.find(substr); //Finding the first occurrence. If not found, following loop won't run.while (pos < str.length()){pos++;total++;cout << "Occurence number " << total << " occurs at position " << pos-1 << endl;pos = str.find(substr, pos);}cout << "Total number of times substring is found in string is : " << total; //Printing the answer}
Line 14: The variable total
is initialised as 0. This will be the variable where we store our final count.
Line 21: Here the find()
method is used for the first time. If no occurrence is found, then the returned value will be the npos of string, otherwise the returned value will be the position of the first occurrence. The returned value will be stored in pos
variable.
Line 22: The while
loop is set, with a condition that pos
is less than the length of the substring — this insures that the returned value is not the npos of string.
Lines 23–24: At every iteration of the loop, the pos
and total
will be incremented. The total
is incremented at every occurrence of substring, as the npos of string wasn’t returned. The pos
is incremented so it can be used with find()
again to find the next occurrence, if any, of the substring.
Line 26: Using the incremented value of pos
, the find()
method is used again to find the next occurrence of the substring. If not found, it will return the npos of string, in which case the loop will end as the returned value will again be stored in pos
.
In this Answer, we learnt how to count the occurrences of the substring within a main string in C++. We saw the usefulness of find()
for this task, and explored the solution code in detail.
Free Resources