-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strncmp.c
80 lines (73 loc) · 2.16 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jcumbe-q <jcumbe-q@student.42barcel> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/22 16:30:41 by jcumbe-q #+# #+# */
/* Updated: 2023/08/30 11:02:09 by jcumbe-q ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int ft_strncmp(char *s1, char *s2, unsigned int n)
{
unsigned int i;
i = 0;
while (s1[i] && s2[i] && i < n)
{
if (s1[i] != s2[i])
{
return ((unsigned char)s1[i] - (unsigned char)s2[i]);
}
i++;
}
if (i == n)
{
return (0);
}
else
{
return ((unsigned char)s1[i] - (unsigned char)s2[i]);
}
}
/*int main(int argc, char **argv)
{
int result;
if (argc == 3)
{
int n = 5; // Si n es 1 se compara hasta el char en pos 0
// char str1[] = "applz";
// char str2[] = "apple";
printf("\n##### ARGS ##### \n");
printf("You have inserted: %d words.\n", argc - 1);
printf("String 1: %s\n", argv[1]);
printf("String 2: %s\n\n", argv[2]);
printf("\n##### IMPLEMENTATION ##### \n");
result = ft_strncmp(argv[1], argv[2], n);
printf("Result: %d\n", result);
printf("\n##### ORIGINAL ##### \n");
result = strncmp(argv[1], argv[2], n);
printf("Result: %d\n\n", result);
if (result < 0)
{
printf("%s es menor que %s\n", argv[1], argv[2]);
}
else if (result == 0)
{
printf("Strings are equal up to %d characters.\n", n);
printf("%s es igual a %s\n", argv[1], argv[2]);
}
else
{
printf("%s es mayor que %s\n", argv[1], argv[2]);
}
}
else
{
printf("Insert just two words!");
}
return (0);
}*/