Friday, December 1, 2017

String - Substring Count

A string S is passed as the input and another string E is also passed as the input. The program must print the number of times E appears in string S. The alphabets in both S and E will be in lower case.

Input Format:
The first line contains S. The second line contains E.

Output Format:
The first line contains the number of times the string E is present in S.

Boundary Conditions: 2 <= Length of a String Value S and E <= 500

Example Input/Output 1:
Input:
Eyeyeyeeyeyeeye
eye
Output:
6

Solution 1:
s,e,c=input(),input(),0
for i in range(len(s)-len(e)+1):
    if s[i:i+len(e)]==e: c+=1
print(c)

Section 2:
#include <iostream>
using namespace std;
int main()
{
 string s,e;
 cin>>s>>e;
 int i,c=0,x=s.size(),y=e.size();
 for(i=0;i<=x-y;i++)
   if(s.substr(i,y)==e) c++;
 cout<<c;
}
Share: