-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrStr.cpp
More file actions
31 lines (29 loc) · 863 Bytes
/
Copy pathstrStr.cpp
File metadata and controls
31 lines (29 loc) · 863 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
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) {
// write your code here
if (source == NULL || target == NULL) {
return -1;
}
string src(source);
string tar(target);
int i, j;
for (i = 0; i < src.length() - tar.length() + 1; i++) {
for (j = 0; j < tar.length(); j++) {
if (source[i + j] != tar[j]) {
break;
}
}
if (j == tar.length()) {
return i;
}
}
return -1;
}
};