From 9e7312327a6fde6e3d52b10cc9361bad934c0275 Mon Sep 17 00:00:00 2001 From: Tanguy MAZE Date: Thu, 5 Apr 2018 13:46:11 +0200 Subject: [PATCH] ft_strncmp --- Makefile | 3 ++- ft_strncmp.c | 28 ++++++++++++++++++++++++++++ libft.h | 4 ++-- 3 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 ft_strncmp.c diff --git a/Makefile b/Makefile index 4615246..8d0a8a4 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: tmaze +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2018/04/04 10:51:59 by tmaze #+# #+# # -# Updated: 2018/04/05 12:34:54 by tmaze ### ########.fr # +# Updated: 2018/04/05 13:16:24 by tmaze ### ########.fr # # # #******************************************************************************# @@ -28,6 +28,7 @@ SRCS = \ ft_strstr.c \ ft_strnstr.c \ ft_strcmp.c \ + ft_strncmp.c \ ft_isalpha.c \ ft_isdigit.c \ ft_isalnum.c \ diff --git a/ft_strncmp.c b/ft_strncmp.c new file mode 100644 index 0000000..e4658b7 --- /dev/null +++ b/ft_strncmp.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/05 13:04:21 by tmaze #+# #+# */ +/* Updated: 2018/04/05 13:15:51 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strncmp(const char *s1, const char *s2, size_t n) +{ + int i; + int diff; + + i = 1; + diff = s1[0] - s2[0]; + while ((unsigned int)i < n && diff == 0 && s1[i - 1] && s2[i - 1]) + { + diff = s1[i] - s2[i]; + i++; + } + return (diff); +} diff --git a/libft.h b/libft.h index f403b25..907f129 100644 --- a/libft.h +++ b/libft.h @@ -6,7 +6,7 @@ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2018/04/04 10:40:01 by tmaze #+# #+# */ -/* Updated: 2018/04/05 11:39:51 by tmaze ### ########.fr */ +/* Updated: 2018/04/05 13:11:35 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,7 +39,7 @@ char *ft_strrchr(const char *s, int c); char *ft_strstr(const char *haystack, const char *needle); char *ft_strnstr(const char *haystack, const char *needle, size_t len); int ft_strcmp(const char *s1, const char *s2); -int ft_strncmp(const char *s1, const char *s2); +int ft_strncmp(const char *s1, const char *s2, size_t n); int ft_atoi(const char *nptr); int ft_isalpha(int c); int ft_isdigit(int c);