aboutsummaryrefslogtreecommitdiff
path: root/ft_strnstr.c
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-10-21 13:23:11 +0200
committerCharles <sircharlesaze@gmail.com>2019-10-21 13:23:11 +0200
commit70a7a3a54d4e300c7ec75d18d14d6d6b174c9ee4 (patch)
tree2c07e9ae3c37aaf477e4baa6e991b8cac55af3cd /ft_strnstr.c
parent2a6b46d026e7ae16af85c4c211fb685f699a5377 (diff)
downloadlibft-70a7a3a54d4e300c7ec75d18d14d6d6b174c9ee4.tar.gz
libft-70a7a3a54d4e300c7ec75d18d14d6d6b174c9ee4.tar.bz2
libft-70a7a3a54d4e300c7ec75d18d14d6d6b174c9ee4.zip
fixed strnstr, added protection on specific functions of part1
Diffstat (limited to 'ft_strnstr.c')
-rw-r--r--ft_strnstr.c14
1 files changed, 5 insertions, 9 deletions
diff --git a/ft_strnstr.c b/ft_strnstr.c
index 5f6f0f1..4ca9f4b 100644
--- a/ft_strnstr.c
+++ b/ft_strnstr.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/07 10:25:13 by cacharle #+# #+# */
-/* Updated: 2019/10/20 14:21:42 by cacharle ### ########.fr */
+/* Updated: 2019/10/21 11:04:56 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -16,20 +16,16 @@
char *ft_strnstr(const char *haystack, const char *needle, size_t len)
{
- size_t i;
- /* size_t j; */
size_t needle_len;
- char *mut_haystack;
needle_len = ft_strlen(needle);
if (needle_len == 0)
return ((char*)haystack);
- mut_haystack = (char*)haystack;
- i = -1;
- while (++i < len && haystack[i])
+ while (*haystack && len-- >= needle_len)
{
- if (ft_strncmp(mut_haystack + i, needle, needle_len) == 0)
- return (mut_haystack + i);
+ if (ft_strncmp(haystack, needle, needle_len) == 0)
+ return ((char*)haystack);
+ haystack++;
}
return (NULL);
}