-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrStr.ORIGINAL.cpp
More file actions
36 lines (36 loc) · 928 Bytes
/
strStr.ORIGINAL.cpp
File metadata and controls
36 lines (36 loc) · 928 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
/*
strStr
Returns the position of the first
occurrence of string target in string source,
or -1 if target is not part of source.
int strStr(String source, String target) {
//…
}
//http://lintcode.com/problem/strstr/
*/
class Solution {
public:
/**
* Returns a index to the first occurrence of target in source,
* or -1 if target is not part of source.
* @param source string to be scanned.
* @param target string containing the sequence of characters to match.
*/
int strStr(const char *source, const char *target) {
if (source == NULL || target == NULL) {
return -1;
}
for (int i = 0; i < strlen(source) - strlen(target) + 1; ++i) {
int j = 0;
for (; j < strlen(target); ++j) {
if (source[i + j] != target[j]) {
break;
}
}
if (j == strlen(target)) {
return i;
}
}
return -1;
}
};