ft_strncmp: initial implementation

This commit is contained in:
Khaïs COLIN 2024-10-16 13:47:09 +02:00
parent 10cf338cbf
commit b176bdaebe
3 changed files with 53 additions and 3 deletions

View file

@ -6,7 +6,7 @@
# By: kcolin <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/14 13:43:59 by kcolin #+# #+# #
# Updated: 2024/10/16 10:46:46 by kcolin ### ########.fr #
# Updated: 2024/10/16 13:22:00 by kcolin ### ########.fr #
# #
# **************************************************************************** #
@ -27,7 +27,8 @@ SOURCES = ft_isalpha.c \
ft_toupper.c \
ft_tolower.c \
ft_strchr.c \
ft_strrchr.c
ft_strrchr.c \
ft_strncmp.c
OBJECTS = $(SOURCES:.c=.o)
CC = gcc

48
ft_strncmp.c Normal file
View file

@ -0,0 +1,48 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/16 13:23:17 by kcolin #+# #+# */
/* Updated: 2024/10/16 13:44:38 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_strncmp(const char *s1, const char *s2, t_size n)
{
t_size i;
i = 0;
while (s1[i] != '\0' && s2[i] != 0)
{
if (i >= n)
return (0);
if (s1[i] != 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)
{
t_size cmp_len;
cmp_len = 9;
if (argc > 2)
{
printf("mine: %d\tlib: %d\n",
ft_strncmp(argv[1], argv[2], cmp_len),
strncmp(argv[1], argv[2], cmp_len));
}
return (0);
}
*/

View file

@ -6,7 +6,7 @@
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 10:11:54 by kcolin #+# #+# */
/* Updated: 2024/10/16 10:34:08 by kcolin ### ########.fr */
/* Updated: 2024/10/16 13:23:08 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
@ -35,5 +35,6 @@ int ft_tolower(int c);
char *ft_strchr(const char *s, int c);
char *ft_strrchr(const char *s, int c);
int ft_strncmp(const char *s1, const char *s2, t_size n);
#endif