ft_memcmp: initial implementation

also fix a small clarity issue in ft_strncmp
This commit is contained in:
Khaïs COLIN 2024-10-16 15:34:30 +02:00
parent b81ad331b5
commit 1c3df31518
4 changed files with 54 additions and 4 deletions

View file

@ -6,7 +6,7 @@
# By: kcolin <marvin@42.fr> +#+ +:+ +#+ # # By: kcolin <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2024/10/14 13:43:59 by kcolin #+# #+# # # Created: 2024/10/14 13:43:59 by kcolin #+# #+# #
# Updated: 2024/10/16 13:50:09 by kcolin ### ########.fr # # Updated: 2024/10/16 15:22:08 by kcolin ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -29,7 +29,8 @@ SOURCES = ft_isalpha.c \
ft_strchr.c \ ft_strchr.c \
ft_strrchr.c \ ft_strrchr.c \
ft_strncmp.c \ ft_strncmp.c \
ft_memchr.c ft_memchr.c \
ft_memcmp.c
OBJECTS = $(SOURCES:.c=.o) OBJECTS = $(SOURCES:.c=.o)
CC = gcc CC = gcc

48
ft_memcmp.c Normal file
View file

@ -0,0 +1,48 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/16 15:26:08 by kcolin #+# #+# */
/* Updated: 2024/10/16 15:31:27 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_memcmp(const void *s1, const void *s2, size_t n)
{
size_t i;
i = 0;
while (1)
{
if (i >= n)
return (0);
if (((unsigned char *)s1)[i] != ((unsigned char *)s2)[i])
break ;
i++;
}
return (((unsigned char *)s1)[i] - ((unsigned char *)s2)[i]);
}
/*
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
size_t cmp_len;
cmp_len = 0;
if (argc > 2)
{
printf("mine: %d\tlib: %d\n",
ft_memcmp(argv[1], argv[2], cmp_len),
memcmp(argv[1], argv[2], cmp_len));
}
return (0);
}
*/

View file

@ -17,7 +17,7 @@ int ft_strncmp(const char *s1, const char *s2, size_t n)
size_t i; size_t i;
i = 0; i = 0;
while (s1[i] != '\0' && s2[i] != 0) while (s1[i] != '\0' && s2[i] != '\0')
{ {
if (i >= n) if (i >= n)
return (0); return (0);

View file

@ -6,7 +6,7 @@
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 10:11:54 by kcolin #+# #+# */ /* Created: 2024/10/15 10:11:54 by kcolin #+# #+# */
/* Updated: 2024/10/16 15:16:50 by kcolin ### ########.fr */ /* Updated: 2024/10/16 15:25:46 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -38,5 +38,6 @@ char *ft_strrchr(const char *s, int c);
int ft_strncmp(const char *s1, const char *s2, size_t n); int ft_strncmp(const char *s1, const char *s2, size_t n);
void *ft_memchr(const void *s, int c, size_t n); void *ft_memchr(const void *s, int c, size_t n);
int ft_memcmp(const void *s1, const void *s2, size_t n);
#endif #endif