-
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.32 KB
/
ft_substr.c
File metadata and controls
37 lines (34 loc) · 1.32 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: ommohame < ommohame@student.42abudhabi.ae> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/28 00:46:59 by ommohame #+# #+# */
/* Updated: 2022/01/13 00:03:52 by ommohame ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
* creates a sub string
* starts from s + start
* copies len times
*/
char *ft_substr(char const *s, unsigned int start, size_t len)
{
size_t s_len;
char *sub;
if (!s)
return (NULL);
s_len = ft_strlen(s);
if (start > s_len)
return (ft_strdup(""));
if (len > s_len - start)
len = s_len - start;
sub = (char *)malloc(sizeof(char) * (len + 1));
if (!sub)
return (NULL);
ft_strlcpy(sub, s + start, len + 1);
return (sub);
}