-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_substr.c
40 lines (36 loc) · 1.51 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcosta-d <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/23 20:42:41 by mcosta-d #+# #+# */
/* Updated: 2023/05/10 23:17:49 by mcosta-d ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *sub;
if (s == NULL)
return (NULL);
if (start > ft_strlen(s))
{
sub = malloc(sizeof(char));
if (sub == NULL)
return (NULL);
*sub = 0;
return (sub);
}
if (len >= ft_strlen(s) - start)
len = ft_strlen(s) - start;
sub = malloc(len + 1);
if (sub == NULL)
return (NULL);
ft_strlcpy(sub, s + start, len + 1);
return (sub);
}
/*verifica se o tamanho que você quer pegar é maior do que o que está disponível
na frase, começando pela posição de início. Se for maior, o algoritmo ajusta o
tamanho para pegar apenas o que está disponível.*/