-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_str.c
85 lines (74 loc) · 1.69 KB
/
ft_str.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
81
82
83
84
85
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_str.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lgillot- <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/19 01:37:00 by lgillot- #+# #+# */
/* Updated: 2015/11/19 01:37:49 by lgillot- ### ########.fr */
/* */
/* ************************************************************************** */
#include <string.h>
#include <stdlib.h>
#include <ft_str.h>
size_t ft_strlen(const char *s)
{
size_t len;
len = 0;
while (*s)
{
s++;
len++;
}
return (len);
}
char *ft_strdup(const char *s1)
{
size_t len;
char *s2;
len = ft_strlen(s1);
s2 = (char *)malloc(len * sizeof(char));
if (!s2)
{
return (NULL);
}
return (ft_strcpy(s2, s1));
}
char *ft_strcpy(char *dst, const char *src)
{
char *dst_orig;
dst_orig = dst;
while (*src)
{
*dst = *src;
src++;
dst++;
}
return (dst_orig);
}
char *ft_strncpy(char *dst, const char *src, size_t n)
{
char *dst_orig;
dst_orig = dst;
while (n)
{
if (*src)
{
*dst = *src;
src++;
}
else
{
*dst = '\0';
}
dst++;
n--;
}
return (dst_orig);
}
char *ft_strcat(char *s1, const char *s2)
{
ft_strcpy(s1 + ft_strlen(s1), s2);
return (s1);
}