piscine/c03/ex01/ft_strncmp.c
Willem Haffmans 607ce08c18 all
2024-09-10 00:18:01 +02:00

52 lines
1.5 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/07 14:39:41 by whaffman #+# #+# */
/* Updated: 2024/06/18 16:26:53 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <string.h>
int ft_strncmp(char *s1, char *s2, unsigned int n)
{
unsigned int i;
i = 0;
// if (n == 0)
// return (0);
while (*s1 && *s1 == *s2 && i < n - 1)
{
s1++;
s2++;
i++;
}
return (*s1 - *s2);
}
#ifdef DEBUG
void test(char *s1, char *s2, unsigned int n)
{
printf("s1: \"%s\"\ns2: \"%s\"\nstrncmp: %d\nft_strncmp: %d\n\n", \
s1, s2, strncmp(s1, s2, n), ft_strncmp(s1, s2, n));
}
int main(void)
{
test("abc", "abc", 3);
test("ab", "abc", 3);
test("abc", "ab", 3);
test("abd", "abc", 3);
test("abd", "abc", 2);
test("abd", "zbc", 0);
test("abd", "zbc", 1);
return (0);
}
#endif