summaryrefslogtreecommitdiff
path: root/ft_strnstr.c
diff options
context:
space:
mode:
authorDominik Kaiser2024-03-10 13:37:45 +0100
committerDominik Kaiser2024-03-10 13:37:45 +0100
commit4e19b82398e0cfaa3ca0b9f68772c88d103ff6ea (patch)
treee25f459959a69578322529aca6d2a02cb2d3dfcf /ft_strnstr.c
parent3c90d29fe5bf9ba6da090cd59c8c139d269f8bd4 (diff)
downloadlibft-4e19b82398e0cfaa3ca0b9f68772c88d103ff6ea.tar.gz
libft-4e19b82398e0cfaa3ca0b9f68772c88d103ff6ea.zip
Complete missing functions and add header
Diffstat (limited to 'ft_strnstr.c')
-rw-r--r--ft_strnstr.c17
1 files changed, 10 insertions, 7 deletions
diff --git a/ft_strnstr.c b/ft_strnstr.c
index 2b96b64..54868d4 100644
--- a/ft_strnstr.c
+++ b/ft_strnstr.c
@@ -6,21 +6,23 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/06 16:07:54 by dkaiser #+# #+# */
-/* Updated: 2024/03/07 16:40:20 by dkaiser ### ########.fr */
+/* Updated: 2024/03/10 13:16:58 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
-#include <string.h>
+#include "libft.h"
char *ft_strnstr(const char *haystack, const char *needle, size_t len)
{
size_t i;
size_t k;
+ if (len < 0 || (!haystack[0] && needle[0]))
+ return (0);
i = 0;
if (*needle == '\0' || needle == haystack)
return ((char *)haystack);
- while (i < len)
+ while (i < len && haystack[i])
{
k = 0;
while (haystack[i + k] && haystack[i + k] == needle[k] && i + k < len)
@@ -38,9 +40,10 @@ char *ft_strnstr(const char *haystack, const char *needle, size_t len)
/* int main(void) */
/* { */
-/* char haystack[] = "M"; */
-/* char needle[] = "MZIRIBMZE"; */
+/* char haystack[] = "abc"; */
+/* char needle[] = "abcde"; */
+/* size_t len = 5; */
-/* printf("strnstr: %s\n", strnstr(haystack, haystack, 2)); */
-/* printf("ft_strnstr: %s\n", ft_strnstr(haystack, haystack, 2)); */
+/* printf("strnstr: %s\n", strnstr(haystack, needle, len)); */
+/* printf("ft_strnstr: %s\n", ft_strnstr(haystack, needle, len)); */
/* } */