-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimpliment_index_function.cpp
More file actions
44 lines (36 loc) · 926 Bytes
/
Copy pathimpliment_index_function.cpp
File metadata and controls
44 lines (36 loc) · 926 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
37
38
39
40
41
42
43
44
#include <iostream>
using namespace std;
int index(const char* str, const char* substr) {
int str_len = 0;
int substr_len = 0;
// Calculate the lengths of the main string and the substring
while (str[str_len] != '\0') {
str_len++;
}
while (substr[substr_len] != '\0') {
substr_len++;
}
for (int i = 0; i <= str_len - substr_len; i++) {
int j;
for (j = 0; j < substr_len; j++) {
if (str[i + j] != substr[j]) {
break;
}
}
if (j == substr_len) {
return i;
}
}
return -1;
}
int main() {
const char* str = "Hello, World!";
const char* substr = "World";
int position = index(str, substr);
if (position != -1) {
cout << "Substring found at index: " << position << endl;
} else {
cout << "Substring not found." << endl;
}
return 0;
}