-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_substr.c
41 lines (38 loc) · 1.38 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ysaito <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/28 19:31:01 by ysaito #+# #+# */
/* Updated: 2021/11/01 21:03:56 by ysaito ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdio.h>
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *new;
size_t slen;
size_t newlen;
size_t i;
if (s == NULL)
return (NULL);
slen = ft_strlen(s);
if (start > slen || len == 0)
return (ft_strdup(""));
newlen = slen - start;
if (newlen > len)
newlen = len;
new = malloc(sizeof(char *) * (newlen + 1));
if (new == NULL)
return (NULL);
i = 0;
while (s[start] != '\0' && newlen-- > 0)
{
new[i++] = s[start++];
}
new[i] = '\0';
return (new);
}