-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_substr.c
44 lines (39 loc) · 1.37 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
42
43
44
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ngordobi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 16:59:11 by ngordobi #+# #+# */
/* Updated: 2024/07/02 12:22:54 by ngordobi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(const char *s, unsigned int start, size_t len)
{
char *sub_s;
size_t i;
if (start >= ft_strlen(s))
return (ft_strdup(""));
if (len >= ft_strlen(s + start))
len = ft_strlen(s + start);
sub_s = malloc(sizeof(char) * (len + 1));
if (sub_s == NULL)
return (NULL);
i = 0;
while (i < len)
{
sub_s[i] = s[start];
i++;
start++;
}
sub_s[i] = '\0';
return (sub_s);
}
/*int main(void)
{
char s[] = "hola";
printf("%s", ft_substr(s, 2, 5));
return (0);
}*/