Answers for "finding frequency of elements in string in c++"

C++
0

frequency of a substring in a string c++

#include <string>
#include <iostream>
int main()
{
   int occurrences = 0;
   std::string::size_type pos = 0;
   std::string s = "FooBarFooBarFoo";
   std::string target = "Foo";
   while ((pos = s.find(target, pos )) != std::string::npos) {
          ++ occurrences;
          pos += target.length();
   }
   std::cout << occurrences << std::endl;

}
Posted by: Guest on October-30-2021

Code answers related to "finding frequency of elements in string in c++"

Browse Popular Code Answers by Language