-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc29.c
More file actions
36 lines (30 loc) · 835 Bytes
/
c29.c
File metadata and controls
36 lines (30 loc) · 835 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
// Implement strstr() (substring search)
#include<stdio.h>
char *myStrStr(const char *string, const char *substr)
{ //cost-> "read only"
if (!*substr) return (char *)string;//(char *)to remove const char temporarily and match expected return type
for (int i = 0; string[i] != '\0'; i++)
{
int j = 0;
while (substr[j] != '\0' && string[i + j] == substr[j])
{
j++;
}
if (substr[j] == '\0')
{
return (char *)(string + i);
}
}
return NULL;
}
int main()
{
const char *text = "codecraX mentors juniors";
const char *search = "mentors";
char *result = myStrStr(text, search);
if (result)
printf("Found at: %s\n", result); // Output: mentors juniors
else
printf("Not found\n");
return 0;
}