-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_substr.c
37 lines (34 loc) · 1.33 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: edesaint <edesaint@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/05 20:46:14 by edesaint #+# #+# */
/* Updated: 2022/12/05 20:46:28 by edesaint ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *nstr;
int size_nstr;
unsigned int len_s;
len_s = ft_strlen(s);
if (start > len_s + 1)
{
size_nstr = 0;
start = 0;
}
else
size_nstr = len_s - start;
if (size_nstr > (int) len)
size_nstr = len;
nstr = malloc(sizeof(char) * (size_nstr + 1));
if (!nstr)
return (NULL);
ft_strlcpy(nstr, s + start, size_nstr + 1);
return (nstr);
}