-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_substr.c
40 lines (37 loc) · 1.34 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: mmita <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/19 13:23:03 by mmita #+# #+# */
/* Updated: 2022/11/19 15:37:26 by mmita ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdio.h>
char *ft_substr(char const *s, unsigned int start, size_t len)
{
unsigned int i;
char *src;
i = 0;
if (start >= ft_strlen(s))
{
src = (char *)malloc(sizeof(*s));
*src = 0;
return (src);
}
if (ft_strlen(s) - start < len)
len = ft_strlen(s) - start;
src = (char *)malloc(sizeof(*s) * (len + 1));
if (!src)
return (NULL);
while (s[start + i] && i < len)
{
src[i] = s[start + i];
i++;
}
src[i] = '\0';
return (src);
}