diff options
| author | Dominik Kaiser | 2024-06-24 16:58:46 +0200 |
|---|---|---|
| committer | GitHub | 2024-06-24 16:58:46 +0200 |
| commit | a28e57c6e0277a1d7346bec58cf557c52e337501 (patch) | |
| tree | 8710c1c12dfd9e6c52821944b0676ec15e5e20ae /lib/libft/ft_strnstr.c | |
| parent | f1349566aef0cb80ef8f4cdcf2795e38631ed820 (diff) | |
| download | minishell-a28e57c6e0277a1d7346bec58cf557c52e337501.tar.gz minishell-a28e57c6e0277a1d7346bec58cf557c52e337501.zip | |
Add libft
Diffstat (limited to 'lib/libft/ft_strnstr.c')
| -rw-r--r-- | lib/libft/ft_strnstr.c | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/lib/libft/ft_strnstr.c b/lib/libft/ft_strnstr.c new file mode 100644 index 0000000..373e90c --- /dev/null +++ b/lib/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)); */ +/* } */ |
