-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_memcmp.c
56 lines (50 loc) · 1.73 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: edegraev <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/10 22:58:54 by edegraev #+# #+# */
/* Updated: 2023/11/11 10:45:47 by edegraev ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_memcmp(const void *s1, const void *s2, size_t n)
{
const unsigned char *str1;
const unsigned char *str2;
size_t i;
if (s1 == s2 || n == 0)
return (0);
str1 = (const unsigned char *)s1;
str2 = (const unsigned char *)s2;
i = 0;
while (i < n)
{
if (*str1 != *str2)
return (*str1 - *str2);
i++;
str1++;
str2++;
}
return (0);
}
// #include <assert.h>
// #include <stdio.h>
// #include <stdlib.h>
// #include <string.h>
// int main()
// {
// int n;
// char buffer1[] = "DWgaOtP12df0";
// char buffer2[] = "DWGAOTP12DF0";
// n = ft_memcmp(buffer1, buffer2, sizeof(buffer1));
// if (n > 0)
// printf("'%s' is greater than '%s'.\n", buffer1, buffer2);
// else if (n < 0)
// printf("'%s' is less than '%s'.\n", buffer1, buffer2);
// else
// printf("'%s' is the same as '%s'.\n", buffer1, buffer2);
// return 0;
// }