diff options
Diffstat (limited to 'ft_strnstr.c')
| -rw-r--r-- | ft_strnstr.c | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/ft_strnstr.c b/ft_strnstr.c new file mode 100644 index 0000000..b91d162 --- /dev/null +++ b/ft_strnstr.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/06 16:07:54 by dkaiser #+# #+# */ +/* Updated: 2024/03/06 16:24:52 by dkaiser ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <string.h> + +char *ft_strnstr(const char *haystack, const char *needle, size_t len) +{ + size_t i; + size_t k; + + i = 0; + while (i < len) + { + k = 0; + while (haystack[i + k] == needle[k]) + { + k++; + } + if (!needle[k]) + return ((char *)haystack + i); + i++; + } + return (0); +} + +/* #include <stdio.h> */ +/* int main() { */ +/* char haystack[] = "Hello world"; */ +/* char needle[] = "d"; */ +/* printf("strnstr: %s\n", strnstr(haystack, needle, 10)); */ +/* printf("ft_strnstr: %s\n", ft_strnstr(haystack, needle, 10)); */ +/* } */ |
