-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_libc_string_locate_and_compare.c
71 lines (63 loc) · 1.19 KB
/
ft_libc_string_locate_and_compare.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
/* ************************************************************************** */
/**//**//*****//**//* By: *//**//* Created: by *//* Updated: by *//**/
/* ************************************************************************** */
#include "libft.h"
char *ft_strchr(const char *s, int c)
{
while (*s)
{
if (*s == (char)c)
return ((char *)s);
s++;
}
if (c == '\0')
return ((char *)s);
return (NULL);
}
char *ft_strrchr(const char *s, int c)
{
size_t i;
i = ft_strlen(s);
if (c == '\0')
return ((char *)&s[i]);
while (i--)
{
if (s[i] == (char)c)
return ((char *)&s[i]);
}
return (NULL);
}
void *ft_memchr(const void *s, int c, size_t n)
{
while (n--)
{
if (*(unsigned char *)s == (unsigned char)c)
return ((void *)s);
s++;
}
return (NULL);
}
int ft_strncmp(const char *s1, const char *s2, size_t n)
{
while (n--)
{
if (*s1 != *s2)
return (*(unsigned char *)s1 - *(unsigned char *)s2);
if (*s1 == '\0' || *s2 == '\0')
break ;
s1++;
s2++;
}
return (0);
}
int ft_memcmp(const void *s1, const void *s2, size_t n)
{
int diff;
while (n--)
{
diff = *((unsigned char *)s1++) - *((unsigned char *)s2++);
if (diff)
return (diff);
}
return (0);
}