-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_substr.c
36 lines (33 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcatal-d <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/28 00:24:43 by mcatal-d #+# #+# */
/* Updated: 2022/11/08 18:06:35 by mcatal-d ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *resultat;
size_t i;
size_t j;
if (!s)
return (0);
if ((size_t)start > ft_strlen(s))
return (ft_strdup(""));
if (len > ft_strlen(s) - start)
len = ft_strlen (s) - start;
resultat = malloc(sizeof(char) * (len + 1));
if (!resultat)
return (0);
i = start;
j = 0;
while (i < ft_strlen(s) && j < len)
resultat[j++] = s[i++];
resultat[j] = '\0';
return (resultat);
}