-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_substr.c
More file actions
41 lines (38 loc) · 1.39 KB
/
ft_substr.c
File metadata and controls
41 lines (38 loc) · 1.39 KB
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: jbadaire <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/24 14:28:46 by jbadaire #+# #+# */
/* Updated: 2023/01/17 17:51:18 by jbadaire ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *mlc;
size_t index;
size_t s_size;
if (!s || s[0] == '\0')
return (ft_strdup(""));
s_size = ft_strlen(s);
if (start >= s_size)
return (ft_strdup(""));
if (len > (s_size - start))
len = s_size - start;
mlc = malloc(sizeof (char) * (len + 1));
if (!mlc)
return (NULL);
index = 0;
while (s[index] && index < len)
{
mlc[index] = s[start];
start++;
index++;
}
mlc[index] = '\0';
return (mlc);
}