-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strncmp.c
44 lines (40 loc) · 1.51 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
41
42
43
44
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hleong <hleong@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/08 16:32:01 by hleong #+# #+# */
/* Updated: 2022/12/08 16:32:28 by hleong ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_strncmp(const char *s1, const char *s2, size_t n)
{
size_t i;
char *str1;
char *str2;
i = 0;
str1 = (char *)s1;
str2 = (char *)s2;
while (str1 != 0 && str2 != 0 && i < n)
{
if (str1[i] != str2[i])
return (str1[i] - str2[i]);
i++;
}
return (0);
}
/* int main (void)
{
const char *s1 = "Hello!";
const char *s2 = "Hello!!";
const char *ft_s1 = "Hello!";
const char *ft_s2 = "Hello!!";
size_t n = 7;
printf("s1-s2= %d\n", strncmp(s1, s2, n));
printf("s2-s1= %d\n", strncmp(s2, s1, n));
printf("ft_s1-ft_s2= %d\n", ft_strncmp(ft_s1, ft_s2, n));
printf("ft_s2-ft_s1= %d\n", ft_strncmp(ft_s2, ft_s1, n));
} */