summaryrefslogtreecommitdiff
path: root/libft/ft_strnstr.c
diff options
context:
space:
mode:
authorDominik Kaiser2024-05-08 13:14:19 +0200
committerDominik Kaiser2024-05-08 13:14:19 +0200
commit95ccc46ad62c59e648679acad8b44ba5d4465e3d (patch)
treec053e4c692039d151cefa26759bee90d31d8c0a0 /libft/ft_strnstr.c
parent42289aa55c7dafe8fffbcdfb0e02f089b7b83afb (diff)
downloadpipex-master.tar.gz
pipex-master.zip
Add libft as dirHEADmaster
Diffstat (limited to 'libft/ft_strnstr.c')
-rw-r--r--libft/ft_strnstr.c47
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)); */
+/* } */