-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strncmp.c
40 lines (38 loc) · 1.45 KB
/
ft_strncmp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tedison <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/04/01 17:27:01 by tedison #+# #+# */
/* Updated: 2021/04/03 12:14:07 by tedison ### ########.fr */
/* */
/* ************************************************************************** */
int ft_strncmp(char *s1, char *s2, unsigned int n)
{
unsigned int count;
unsigned char s1_char;
unsigned char s2_char;
count = 0;
s1_char = s1[count];
s2_char = s2[count];
if (n == 0)
return (0);
while (!(s1[count] == '\0' || s2[count] == '\0') && count < n)
{
s1_char = s1[count];
s2_char = s2[count];
if (s1_char != s2_char)
return (s1_char - s2_char);
if (count++ == n - 1 && s1_char == s2_char)
return (0);
}
if ((s1[count] < 0 || s2[count] < 0))
{
s1_char = s1[count];
s2_char = s2[count];
return (s1_char - s2_char);
}
return (s1[count] - s2[count]);
}