#include <stdio.h>
int strstr_custom(const char *haystack, const char *needle) {
int index = 0;
const char *n = needle;
while (*haystack) {
const char *h = haystack;//imp Reset the pointers to the current position in the string
while (*n && (*h == *n)) {//*h++ or h++ both same
*h++;
n++;
}
if (*n == '\0') {
return index;
}
*haystack++;
index++;
}
return -1;
}
int main() {
const char *haystack = "Hello, World!";
const char *needle = "World";
int result = strstr_custom(haystack, needle);
printf("Substring found at position: %d\n", result);
return 0;
}
#if 0
#include <stdio.h>
int strsstr(char a[], char f[]){
int c = 0;
for(int i=0;a[i]!='\0';i++){
char *b = a;
while((*b==*f) && *f){//imp
*b++;
*f++;
}
if(*f=='\0'){
return c;
}
*a++;
c++;
}
return -1;
}
int main() {
char a[30] = "my name is adarsh";
char f[10] = "name";
int len = strsstr(a,f);
printf("%d",len);
}
#endif
To embed this program on your website, copy the following code and paste it into your website's HTML: