-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImplement-strstr
49 lines (33 loc) · 1008 Bytes
/
Implement-strstr
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
#include<iostream>
using namespace std;
//Function which is doing same thing as strstr()
//Find substring in string, and return pointer on place where substring in string starts
const char* strstr1(const char* string, const char* substring){
const char *Pointer = nullptr;
int lenghtOf_Substring = strlen(substring);
int lenghtOf_String = strlen(string);
int counter = 0, counterOf_Correct = 0;
for (int i = 0; i < lenghtOf_String; i++) {
if (string[i] == substring[counter++]) {
counterOf_Correct++;
if (counter == lenghtOf_Substring) {
counter = 0;
}
if (counterOf_Correct == lenghtOf_Substring) {
Pointer = &string[i - lenghtOf_Substring + 1];
break;
}
}
else {
counter = 0;
counterOf_Correct = 0;
}
}
return Pointer;
}
void main() {
//Test it yourselves ;)
//It would return nullptr if string doesn't contain substring
//if (strstr1("Insert", "Insert") != nullptr) { cout << strstr1("Insert", "Insert"); }
system("pause>0");
}