-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_substr.c
executable file
·46 lines (41 loc) · 1.42 KB
/
ft_substr.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kichlee <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/16 17:30:06 by kichlee #+# #+# */
/* Updated: 2022/12/16 19:18:23 by kichlee ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlen_d(const char *ch)
{
size_t i;
i = 0;
while (ch[i])
++i;
return (i);
}
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *string;
size_t size;
size_t i;
i = 0;
if (start > ft_strlen_d(s))
size = 0;
else if (len > ft_strlen_d(s + start))
size = ft_strlen_d(s + start);
else
size = len;
string = (char *)malloc(sizeof(char) * (size + 1));
if (!string || !s)
return (NULL);
if (size)
while (i < len && s[start])
string[i++] = s[start++];
string[i] = '\0';
return (string);
}