From 3151f1574a36491c84a5b90fba4f116fbd54098c Mon Sep 17 00:00:00 2001 From: Tanguy MAZE Date: Sun, 8 Apr 2018 14:08:25 +0200 Subject: [PATCH] ft_strncmp: add unsigned char cmp --- ft_strncmp.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/ft_strncmp.c b/ft_strncmp.c index e4658b7..feb1839 100644 --- a/ft_strncmp.c +++ b/ft_strncmp.c @@ -6,7 +6,7 @@ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2018/04/05 13:04:21 by tmaze #+# #+# */ -/* Updated: 2018/04/05 13:15:51 by tmaze ### ########.fr */ +/* Updated: 2018/04/08 14:05:37 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,10 +18,13 @@ int ft_strncmp(const char *s1, const char *s2, size_t n) int diff; i = 1; - diff = s1[0] - s2[0]; - while ((unsigned int)i < n && diff == 0 && s1[i - 1] && s2[i - 1]) + diff = 0; + if (n > 0) + diff = (unsigned char)s1[0] - (unsigned char)s2[0]; + while ((unsigned int)i < n && diff == 0 && (unsigned char)s1[i - 1] &&\ + (unsigned char)s2[i - 1]) { - diff = s1[i] - s2[i]; + diff = (unsigned char)s1[i] - (unsigned char)s2[i]; i++; } return (diff);