string count occurrences c++
#include <algorithm>
std::string s = "a_b_c";
size_t n = std::count(s.begin(), s.end(), '_'); //n=2
string count occurrences c++
#include <algorithm>
std::string s = "a_b_c";
size_t n = std::count(s.begin(), s.end(), '_'); //n=2
count word accurances in a string c++
string s;
string sub;
int i=0, j, temp, counter=0, chk;
cout<<"Enter the String: ";
getline (cin, s);
cout<<"Enter the Word: ";
getline (cin,sub);
while(s[i]!='\0')
{
temp = i;
j=0;
while(sub[j]!='\0')
{
if(s[i]==sub[j])
i++;
j++;
}
chk = i-temp;
if(chk==j)
counter++;
i = temp;
i++;
}
cout<<"\nOccurrence of '"<<sub<<"' = "<<counter;
cout<<endl;
return 0;
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us