-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strcmp.c
62 lines (58 loc) · 1.94 KB
/
ft_strcmp.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mbutt <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/25 22:08:01 by mbutt #+# #+# */
/* Updated: 2019/03/12 15:10:45 by mbutt ### ########.fr */
/* */
/* ************************************************************************** */
/*
** The strcmp() and strncmp() functions lexicographically compare the
** null-terminated strings s1 and s2.
** RETURN VALUES: The strcmp() and strncmp() functions return an integer greater
** than, equal to, or less than 0, according as the string s1 is greater than,
** equal to, or less than the string s2. The comparison is done using unsigned
** characters, so that `\200' is greater than `\0'.
*/
#include "libft.h"
int ft_strcmp(const char *s1, const char *s2)
{
int i;
i = 0;
while (s1[i] || s2[i])
{
if (s1[i] != s2[i])
return ((unsigned const char)s1[i] - (unsigned const char)s2[i]);
i++;
}
return (0);
}
/*
** Method 2 by using pointers
** int ft_strcmp(const char *s1, const char *s2)
** {
** while (*s1 || *s2)
** {
** if (*s1 != *s2)
** {
** return (*(unsigned const char *)s1 - *(unsigned const char *)s2);
** }
** s1++;
** s2++;
** }
** return (0);
** }
*/
/*
** int main (void)
** {
** char *string1 = "Test";
** char *string2 = "ABCD";
** printf("%d", strcmp(string1, string2));
** printf("\n%d", ft_strcmp(string1, string2));
** return(0);
** }
*/