-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_substr.c
More file actions
37 lines (34 loc) · 1.33 KB
/
ft_substr.c
File metadata and controls
37 lines (34 loc) · 1.33 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bmetehri <bmetehri@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/21 14:28:20 by bmetehri #+# #+# */
/* Updated: 2023/01/05 19:12:10 by bmetehri ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *str;
char *orig;
size_t i;
orig = (char *)s;
if (start >= ft_strlen(orig) || !len)
return (ft_calloc(1, 1));
if (len + start > ft_strlen(orig))
len = ft_strlen(orig) - start;
str = ft_calloc((len + 1) * sizeof(char), 1);
if (!str)
return (NULL);
str[len] = '\0';
i = 0;
while (orig[i + start] && i < len)
{
str[i] = orig[start + i];
i++;
}
return (str);
}