-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_substr.c
40 lines (37 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: oelkhiar <oelkhiar@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/20 13:35:30 by oelkhiar #+# #+# */
/* Updated: 2022/11/20 13:29:06 by oelkhiar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *str;
size_t i;
size_t len_s;
i = 0;
if (s == NULL)
return (NULL);
len_s = ft_strlen(s);
if (start >= len_s)
return (ft_strdup(""));
if (ft_strlen(s + start) < len)
str = malloc(ft_strlen(s + start) * sizeof(char) + 1);
else
str = malloc(len * sizeof(char) + 1);
if (str == NULL )
return (NULL);
while (i < len && s[start + i])
{
str[i] = s[start + i];
i++;
}
str[i] = '\0';
return (str);
}