From fc457992114e71989e5cdeab452bc054a400a397 Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 10 Feb 2020 04:24:29 +0100 Subject: Fixing ft_strcmp, Added ft_strcasecmp, ft_strncasecmp --- src/str/ft_strcasecmp.c | 24 ++++++++++++++++++++++++ src/str/ft_strcmp.c | 9 +++++++-- src/str/ft_strncasecmp.c | 26 ++++++++++++++++++++++++++ src/str/ft_strncmp.c | 5 +++-- src/str/ft_strtolower.c | 26 ++++++++++++++++++++++++++ src/str/ft_strtoupper.c | 26 ++++++++++++++++++++++++++ 6 files changed, 112 insertions(+), 4 deletions(-) create mode 100644 src/str/ft_strcasecmp.c create mode 100644 src/str/ft_strncasecmp.c create mode 100644 src/str/ft_strtolower.c create mode 100644 src/str/ft_strtoupper.c (limited to 'src/str') diff --git a/src/str/ft_strcasecmp.c b/src/str/ft_strcasecmp.c new file mode 100644 index 0000000..45ba592 --- /dev/null +++ b/src/str/ft_strcasecmp.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcasecmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/10 04:08:38 by cacharle #+# #+# */ +/* Updated: 2020/02/10 04:22:56 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" +#include "libft_types.h" + +int strcasecmp(const char *s1, const char *s2) +{ + while (*s1 && *s2 && ft_tolower(*s1) == ft_tolower(*s2)) + { + s1++; + s2++; + } + return ((t_ftuchar)ft_tolower(*s1) - (t_ftuchar)ft_tolower(*s2)); +} diff --git a/src/str/ft_strcmp.c b/src/str/ft_strcmp.c index 1978286..aced711 100644 --- a/src/str/ft_strcmp.c +++ b/src/str/ft_strcmp.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:16:07 by cacharle #+# #+# */ -/* Updated: 2019/11/21 01:58:27 by cacharle ### ########.fr */ +/* Updated: 2020/02/10 04:18:11 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,5 +14,10 @@ int ft_strcmp(const char *s1, const char *s2) { - return (ft_memcmp(s1, s2, ft_strlen(s1) + 1)); + while (*s1 && *s2 && *s1 == *s2) + { + s1++; + s2++; + } + return (*s1 - *s2); } diff --git a/src/str/ft_strncasecmp.c b/src/str/ft_strncasecmp.c new file mode 100644 index 0000000..1b6308d --- /dev/null +++ b/src/str/ft_strncasecmp.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncasecmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/10 04:18:36 by cacharle #+# #+# */ +/* Updated: 2020/02/10 04:21:10 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include "libft_types.h" + +int strncasecmp(const char *s1, const char *s2, size_t n) +{ + size_t i; + + if (n == 0) + return (0); + i = 0; + while (i + 1 < n && s1[i] && ft_tolower(s1[i]) == ft_tolower(s2[i])) + i++; + return ((t_ftuchar)ft_tolower(s1[i]) - (t_ftuchar)ft_tolower(s2[i])); +} diff --git a/src/str/ft_strncmp.c b/src/str/ft_strncmp.c index cd303fc..caa052b 100644 --- a/src/str/ft_strncmp.c +++ b/src/str/ft_strncmp.c @@ -6,11 +6,12 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:27:34 by cacharle #+# #+# */ -/* Updated: 2019/11/21 01:56:32 by cacharle ### ########.fr */ +/* Updated: 2020/02/10 04:17:50 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" +#include "libft_types.h" int ft_strncmp(const char *s1, const char *s2, size_t n) { @@ -21,5 +22,5 @@ int ft_strncmp(const char *s1, const char *s2, size_t n) i = 0; while (i + 1 < n && s1[i] == s2[i] && s1[i]) i++; - return ((unsigned char)s1[i] - (unsigned char)s2[i]); + return ((t_ftuchar)s1[i] - (t_ftuchar)s2[i]); } diff --git a/src/str/ft_strtolower.c b/src/str/ft_strtolower.c new file mode 100644 index 0000000..2eb34c2 --- /dev/null +++ b/src/str/ft_strtolower.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtolower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/10 04:10:01 by cacharle #+# #+# */ +/* Updated: 2020/02/10 04:12:21 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" +#include "libft_ctype.h" + +char *ft_strtolower(char *s) +{ + int i; + + if (s == NULL) + return (NULL); + i = -1; + while (s[i]) + s[i] = ft_tolower(s[i]); + return (s); +} diff --git a/src/str/ft_strtoupper.c b/src/str/ft_strtoupper.c new file mode 100644 index 0000000..d4c49d5 --- /dev/null +++ b/src/str/ft_strtoupper.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtoupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/10 04:12:04 by cacharle #+# #+# */ +/* Updated: 2020/02/10 04:12:17 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" +#include "libft_ctype.h" + +char *ft_strtolower(char *s) +{ + int i; + + if (s == NULL) + return (NULL); + i = -1; + while (s[i]) + s[i] = ft_tolower(s[i]); + return (s); +} -- cgit