int ft_strncmp(char *s1, char *s2, unsigned int n)
{
unsigned int i;
i = 0;
while (s1[i] || s2[i] && (i < n))
{
if (s1[i] == '\0')
return ((s2[i]) * (-1));
if (s2[i] == '\0')
return (s1[i]);
if ((s1[i] > s2[i]) || (s1[i] < s2[i]))
return (s1[i] - s2[i]);
i++;
}
return (0);
}
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
char *s1;
char *s2;
char *s3;
unsigned int lenght;
lenght = 6;
s1 = "Hello"; // l en ascii 108
s2 = "Heklo"; // k en ascii 107
printf("%d\n", ft_strncmp(s1, s2, lenght)); // 108 - 101 = 1
printf("%d\n", strncmp(s1, s2, lenght));
s1 = "Coucou!";
s2 = "Couc";
printf("%d\n", ft_strncmp(s1, s2, lenght)); // 111 car o premier caractere en plus o en asci 111
printf("%d\n", strncmp(s1, s2, lenght)); //111 o en asci 111
s1 = "Cou";
s2 = "Coucou!";
printf("%d\n", ft_strncmp(s1, s2, lenght)); // -66 car en ascii c = 66
printf("%d\n", strncmp(s1, s2, lenght)); // -66
return (0);
}
To embed this project on your website, copy the following code and paste it into your website's HTML: