In the previous article, we have discussed about Find and Replace all occurrences of a sub string in C++. Let us learn how to find all occurrences of a sub string in a string in C++ Program.
Finding all occurrences of a sub string in a string both Case Sensitive & InSensitive in C++
This article is all about finding all occurrences of a sub string in both case sensitive and insensitive manner.
- Find All Case occurrences of a Sub String in a given String- Case Sensitive
- Find all occurrence of a Sub String in a given String- Case Insensitive
Find All Case occurrences of a Sub String in a given String- Case Sensitive :
We will use std::string::find
to search the first occurrence then go on searching like this upto last.
#include <iostream> #include <string> #include <algorithm> #include <vector> void hasOccuredAtIndex(std::vector<size_t> &vec, std::string str, std::string subStr) { // Adding the first occurence of the substring to the vector size_t loc = str.find(subStr); while (loc != std::string::npos) { vec.push_back(loc); //Finding and adding the ext occurence of the substring loc = str.find(subStr, loc + subStr.size()); } } int main(int argc, char const *argv[]) { std::string str = "We are going through an example. We will be seeing how many times the word \'we\' occurs"; std::string subStr = "We"; std::vector<size_t> vec; hasOccuredAtIndex(vec, str, subStr); //The string and substring were passed with a vector which will store the indices of occurence std::cout << "The indices where the string \"" << subStr << "\" has occured" << std::endl; for (size_t loc : vec) std::cout << loc << std::endl; return 0; }
Output : The indices where the string "We" has occured 0 33
The find( )
function is case-sensitive so we could only get the indices of two occurrences of our substring.
Find all occurrence of a Sub String in a given String- Case Insensitive :
Here we are going to use the same approach as the previous example but we will modify the find( )
function to make it case insensitive. We will modify it by creating another function that will take the string and convert them to a lowercase which will solve our case issue.
#include <iostream> #include <string> #include <algorithm> #include <vector> size_t modifiedFind(std::string str, std::string subStr, size_t loc = 0) { // Converting the whole string into lower case std::transform(str.begin(), str.end(), str.begin(), ::tolower); std::transform(subStr.begin(), subStr.end(), subStr.begin(), ::tolower); return str.find(subStr, loc); } void hasOccuredAtIndex(std::vector<size_t> &vec, std::string str, std::string subStr) { // Adding the first occurence of the substring to the vector size_t loc = modifiedFind(str, subStr); while (loc != std::string::npos) { vec.push_back(loc); //Finding and adding the ext occurence of the substring loc = modifiedFind(str, subStr, loc + subStr.size()); } } int main(int argc, char const *argv[]) { std::string str = "We are going through an example. we will be seeing how many times the word \'We\' occurs"; std::string subStr = "We"; std::vector<size_t> vec; hasOccuredAtIndex(vec, str, subStr); //The string and substring were passed with a vector which will store the indices of occurence std::cout << "The indices where the string \"" << subStr << "\" has occured" << std::endl; for (size_t loc : vec) std::cout << loc << std::endl; return 0; }
Output : The indices where the string "We" has occured 0 33 76
Now we can see the modified program has become case insensitive and displays all the occurrences of our substring.