-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStrStr.cpp
60 lines (46 loc) · 1.34 KB
/
StrStr.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
Link:- https://practice.geeksforgeeks.org/problems/implement-strstr/1
Problem:- Your task is to implement the function strstr. The function takes two strings as arguments (s,x) and locates the occurrence of the string x in the string s. The function returns and integer denoting the first occurrence of the string x in s (0 based indexing).
Note: You are not allowed to use inbuilt function.
Input:
s = GeeksForGeeks, x = Fr
Output: -1
Explanation: Fr is not present in the
string GeeksForGeeks as substring.
Input:
s = GeeksForGeeks, x = For
Output: 5
Explanation: For is present as substring
in GeeksForGeeks from index 5 (0 based
indexing).
*/
/****** Using String Matching Naive Algo */
if(x.length()>s.length())
{
return -1;
}
for(int i=0;i<=s.length()-x.length();i++)
{
for(int j=0;j<x.length();j++)
{
if(s[i+j]!=x[j])
{
break;
}
else if(j==x.length()-1)
{
return i;
}
}
}
return -1;
// ******** Using inbuild Function find() ********
// The function returns the index of the first occurrence of sub-string, if the sub-string is not found it returns string::npos
if(s.find(x)!=string::npos)
{
return s.find(x);
}
else
{
return -1;
}