-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_memcmp.c
36 lines (33 loc) · 1.39 KB
/
ft_memcmp.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tedison <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/04/02 13:32:09 by tedison #+# #+# */
/* Updated: 2021/04/05 11:28:35 by tedison ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_memcmp(const void *s1, const void *s2, size_t n)
{
unsigned int count;
unsigned char s1_char;
unsigned char s2_char;
count = 0;
s1_char = ((char *)s1)[count];
s2_char = ((char *)s2)[count];
if (n == 0)
return (0);
while (count < n)
{
s1_char = ((char *)s1)[count];
s2_char = ((char *)s2)[count];
if (s1_char != s2_char)
return (s1_char - s2_char);
if (count++ == n - 1 && s1_char == s2_char)
return (0);
}
return (((unsigned char *)s1)[count] - ((unsigned char *)s2)[count]);
}