Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

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;

}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #frequency #substring #string
ADD COMMENT
Topic
Name
3+4 =