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;