-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_substr.c
40 lines (37 loc) · 1.36 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: abellakr <abellakr@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/10 22:50:55 by abellakr #+# #+# */
/* Updated: 2021/12/09 00:28:47 by abellakr ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *str;
size_t str_index;
if (!s)
return (NULL);
if (len > ft_strlen(s))
len = ft_strlen(s);
if ((size_t)start > ft_strlen(s))
return (ft_strdup(""));
str = (char *)malloc((len + 1) * sizeof(char));
if (str)
{
str_index = 0;
while (len-- && ((char *)s)[start])
{
str[str_index] = ((char *)s)[start];
str_index++;
start++;
}
str[str_index] = 0;
return (str);
}
return (NULL);
}