-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsubstring.cpp
55 lines (44 loc) · 1.49 KB
/
substring.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
#include <iostream>
#include <string>
/*
Apple LLVM version 8.1.0 (clang-802.0.42)
References:
c_str : http://www.cplusplus.com/reference/string/string/c_str/
find : http://www.cplusplus.com/reference/string/string/find/
length : http://www.cplusplus.com/reference/string/string/length/
Compile:
g++ -o substring substring.cpp
*/
int FindSubstringindex(const std::string *parentstring, const std::string *substring) {
int e = 0, indx = -1;
const char *parentcstring = parentstring->c_str();
const char *csubstring = substring->c_str();
for (int i = 0; i < strlen(parentcstring); i++) {
if (parentcstring[i] == csubstring[e]) {
if (e == 0)
indx = i;
e++;
if (e == strlen(csubstring)) {
return indx;
} else {
e = 0;
}
}
return -1;
}
int main () {
const std::string extendedParentstring = "Hello extended string Agnosticdev, I love Tutorials";
const std::string substring = "Agnosticdev";
// Standard Approach
/*
std::string parentstring = "Hello Agnosticdev, I love Tutorials";
if (std::size_t index = parentstring.find(substring)) {
std::cout << "Substring found at index " << index << ", with length " << substring.length() << std::endl;
} else {
std::cout << "Substring not found at all in parent string" << std::endl;
}*/
// Alternative Approach
int substringIndex = FindSubstringindex(&extendedParentstring, &substring);
std::cout << "Substring found at index " << substringIndex << std::endl;
return 0;
}