-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_substr.c
38 lines (35 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pmaimait <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/09 18:15:59 by pmaimait #+# #+# */
/* Updated: 2022/05/26 16:49:44 by pmaimait ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *substr;
char *new_str;
size_t s_len;
size_t i;
s_len = ft_strlen(s);
if (s == NULL || s_len < start)
return (ft_strdup(""));
if (start + len < s_len)
substr = (char *)malloc((len + 1) * sizeof(char));
else
substr = (char *)malloc((s_len - start + 1) * sizeof(char));
if (substr == NULL)
return (NULL);
i = start;
new_str = substr;
while (i < (start + len) && *(s + i))
*new_str++ = *(s + i++);
*new_str = '\0';
return (substr);
}