Expected Outcomes: Enter two strings: abababab bab The occurrence of bab in abababab is: 3 I have tried it, but something wrong in my code, idk what. Here is it:
#include <iostream>
#include <string>
using namespace std;
int occurence(string s1, string s2) {
int count,k;
string x;
for (int i = 0; i < s1.length(); i++)
{
k = 0;
for (int j = 0; j < s2.length(); j++) {
x[j] = s1[i];
}
for (int z = 0; z < s2.length(); z++) {
if (s2[z] == x[z]) {
k++;
}
}
if (k == s2.length()) {
count++
}
}
return count;
}
int main() {
cout << "Enter two strings: ";
string w1, w2;
cin >> w1 >> w2;
int occur=occurence(w1,w2);
cout << "The occurrence of " << w2 << " in " << w1 << " is " << occur;
}