fork download
  1. #include <stdio.h>
  2. char* my_strstr(char *a, char *b);
  3. int main(void)
  4. {
  5. char *haystack = "Stacy Lives Next Door Fool";
  6. char *needle = "Stb";
  7. char *ptr = NULL;
  8. ptr = my_strstr(haystack, needle);
  9. printf("%s\n", ptr);
  10. return (0);
  11. }
  12.  
  13. char* my_strstr(char *haystack, char *needle)
  14. {
  15. int i;
  16. char *a = "does not exist";
  17. if (!needle || !*needle)
  18. return (haystack); // if s2 is empty
  19. while (*haystack != '\0')
  20. {
  21. i = 0;
  22. while (*haystack != *needle && *haystack != '\0')
  23. haystack++;
  24. while (haystack[i] == needle[i] && haystack[i] != '\0')
  25. i++;
  26. if (needle[i] == '\0')
  27. {
  28. printf("\n In this loop\n");
  29. return (haystack);
  30. }
  31.  
  32. haystack++;
  33. }
  34. return a;
  35. }
Success #stdin #stdout 0s 2112KB
stdin
Standard input is empty
stdout
does not exist