-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubstring.cpp
More file actions
46 lines (39 loc) · 786 Bytes
/
substring.cpp
File metadata and controls
46 lines (39 loc) · 786 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// { Driver Code Starts
#include<bits/stdc++.h>
using namespace std;
int strstr(string ,string);
int main()
{
int t;
cin>>t;
while(t--)
{
string a;
string b;
cin>>a;
cin>>b;
cout<<strstr(a,b)<<endl;
}
}
// } Driver Code Ends
/* The function should return position where the target string
matches the string str
Your are required to complete this method */
int strstr(string s, string x)
{
//Your code here
int n=s.length();
int l=x.length();
for(int i=0;i<=n-l;i++){
int j;
for( j=0;j<l;j++){
if(s[i+j]!=x[j]){
break;
}
}
if(j==l){
return i;
}
}
return -1;
}