-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_memcmp.c
37 lines (34 loc) · 1.32 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
37
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: chrhuang <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/07 12:43:40 by chrhuang #+# #+# */
/* Updated: 2018/11/12 14:49:43 by chrhuang ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_memcmp(const void *str1, const void *str2, size_t n)
{
unsigned char *tmp1;
unsigned char *tmp2;
tmp1 = (unsigned char *)str1;
tmp2 = (unsigned char *)str2;
while (n != 0)
{
if (*tmp1 != *tmp2)
return (*tmp1 - *tmp2);
tmp1 = tmp1 + 1;
tmp2 = tmp2 + 1;
n = n - 1;
}
if (n == 0)
return (0);
if (*tmp1 == 0 && *tmp2 != 0)
return (-(*tmp2));
else if (*tmp2 == 0 && *tmp1 != 0)
return (*tmp1);
return (0);
}