-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_substr.c
38 lines (35 loc) · 1.32 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: retahri <retahri@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/31 05:18:14 by retahri #+# #+# */
/* Updated: 2024/11/02 14:58:49 by retahri ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
size_t idx;
char *str;
idx = 0;
if (!s || len == 0)
return (ft_strdup(""));
if (start >= ft_strlen(s))
return (ft_strdup(""));
if (len > ft_strlen(s) - start)
len = ft_strlen(s) - start;
str = malloc(len + 1);
if (!str)
return (ft_strdup(""));
while (s[start] && idx < len)
{
str[idx] = s[start];
idx++;
start++;
}
str[idx] = '\0';
return (str);
}