diff options
| author | Dominik Kaiser | 2024-05-08 13:14:19 +0200 |
|---|---|---|
| committer | Dominik Kaiser | 2024-05-08 13:14:19 +0200 |
| commit | 95ccc46ad62c59e648679acad8b44ba5d4465e3d (patch) | |
| tree | c053e4c692039d151cefa26759bee90d31d8c0a0 /libft/ft_strnstr.c | |
| parent | 42289aa55c7dafe8fffbcdfb0e02f089b7b83afb (diff) | |
| download | pipex-95ccc46ad62c59e648679acad8b44ba5d4465e3d.tar.gz pipex-95ccc46ad62c59e648679acad8b44ba5d4465e3d.zip | |
Diffstat (limited to 'libft/ft_strnstr.c')
| -rw-r--r-- | libft/ft_strnstr.c | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/libft/ft_strnstr.c b/libft/ft_strnstr.c new file mode 100644 index 0000000..373e90c --- /dev/null +++ b/libft/ft_strnstr.c @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/06 16:07:54 by dkaiser #+# #+# */ +/* Updated: 2024/03/11 15:04:28 by dkaiser ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strnstr(const char *haystack, const char *needle, size_t len) +{ + size_t i; + size_t k; + + if (!*haystack && *needle) + return (0); + i = 0; + if (*needle == '\0' || needle == haystack) + return ((char *)haystack); + while (i < len && haystack[i]) + { + k = 0; + while (haystack[i + k] && haystack[i + k] == needle[k] && i + k < len) + k++; + if (!needle[k]) + return ((char *)haystack + i); + i++; + } + return (0); +} + +/* #include <stdio.h> */ + +/* int main(void) */ +/* { */ +/* char haystack[] = "abc"; */ +/* char needle[] = "abcde"; */ +/* size_t len = 5; */ + +/* printf("strnstr: %s\n", strnstr(haystack, needle, len)); */ +/* printf("ft_strnstr: %s\n", ft_strnstr(haystack, needle, len)); */ +/* } */ |
