-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_substr.c
40 lines (37 loc) · 1.31 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: mucankir <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/23 02:42:52 by mucankir #+# #+# */
/* Updated: 2024/11/07 19:01:56 by mucankir ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
int i;
char *sub;
size_t len_s;
if (!s)
return (NULL);
len_s = ft_strlen(s);
if (start >= len_s)
return (ft_strdup(""));
if (len > len_s - start)
len = len_s - start;
i = 0;
sub = (char *)malloc(len + 1);
if (!sub)
return (NULL);
while (*(s + start) && len-- > 0)
{
sub[i] = *(s + start);
i++;
s++;
}
sub[i] = '\0';
return (sub);
}