-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_substr.c
37 lines (34 loc) · 1.36 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yuske <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/01 20:35:39 by yuske #+# #+# */
/* Updated: 2022/11/23 11:18:41 by yuske ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
//17L
//2nd arg is unsigned int. why? compatible with size_t.
//s->str, len->len_subの方がわかりやすい?
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *sub;
size_t end_s;
size_t i;
if (!s)
return (NULL);
end_s = ft_strlen(s);
if (len > end_s)
len = end_s;
sub = (char *)malloc(sizeof(char) * (len + 1));
if (!sub)
return (NULL);
i = 0;
while (i < len && start < end_s)
sub[i++] = (char)s[start++];
sub[i] = '\0';
return (sub);
}