-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_substr.c
41 lines (38 loc) · 1.38 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: taretiuk <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/20 18:12:01 by taretiuk #+# #+# */
/* Updated: 2023/12/04 12:43:56 by taretiuk ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *dest;
int i;
size_t s_length;
if (!s)
return (NULL);
s_length = ft_strlen(s);
if ((size_t)start > s_length)
len = 0;
else if (len > (s_length - start))
len = s_length - start;
dest = (char *) malloc(sizeof(char) * (len + 1));
if (!dest)
return (NULL);
i = 0;
while (start < s_length && s[start] != '\0' && len > 0)
{
dest[i] = s[start];
i++;
start++;
len--;
}
dest[i] = '\0';
return (dest);
}