/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_strstr.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: whaffman +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/11 14:58:08 by whaffman #+# #+# */ /* Updated: 2024/06/11 14:58:17 by whaffman ### ########.fr */ /* */ /* ************************************************************************** */ #include #include int compare(char *str, char *to_find) { int found; found = 0; while (*to_find) { if (*str != *to_find) { found = 0; break ; } else { str++; to_find++; found = 1; } } return (found); } char *ft_strstr(char *str, char *to_find) { int found; if (!*to_find) return (str); found = 0; while (*str) { if (compare(str, to_find)) return (str); str++; } return (NULL); } #ifdef DEBUG void test(char *str, char *to_find) { printf("haystack: \"%s\"\nneedle: \"%s\"\n", \ str, to_find); printf("index(ft_strstr): %ld\nindex(strstr): %ld\n", \ ft_strstr(str, to_find) - str, strstr(str, to_find) - str); } int main(void) { test("Hello, World!", "o, "); test("Hello, World!", "o"); test("Hello, World!", ""); test("Hello, World!", "H"); test("Hello, World!", "Z"); return (0); } #endif