-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_substr.c
42 lines (39 loc) · 1.39 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: phelebra <xhelp00@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/13 10:39:00 by phelebra #+# #+# */
/* Updated: 2023/01/23 09:26:15 by phelebra ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *sub;
unsigned int i;
if ((size_t)start + len > ft_strlen(s))
len = ft_strlen(s) - (size_t)start;
if ((size_t)start >= ft_strlen(s))
{
sub = malloc(sizeof(char));
if (!sub)
return (NULL);
sub[0] = 0;
return (sub);
}
sub = malloc(sizeof(char) * len + 1);
if (!sub)
return (NULL);
i = 0;
while (s[start] != '\0' && i < (unsigned int) len)
{
sub[i] = s[start];
start++;
i++;
}
sub[i] = '\0';
return (sub);
}