-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_substr.c
47 lines (43 loc) · 1.48 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
41
42
43
44
45
46
47
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jkahvedj <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/30 23:21:25 by jkahvedj #+# #+# */
/* Updated: 2021/06/12 20:13:54 by jkahvedj ### ########.fr */
/* */
/* ************************************************************************** */
/*
** LIBRARY: none
**
** DESCRIPTION:
** create new string with the substring of a string
*/
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *dst;
size_t i;
size_t s_len;
i = 0;
if (!s)
return (NULL);
s_len = ft_strlen(s);
if (start >= s_len)
dst = (char *)malloc(sizeof(char) * 1);
else if (s_len > (start + len))
dst = (char *)malloc(sizeof(char) * (len + 1));
else
dst = (char *)malloc(sizeof(char) * (s_len - start + 1));
if (!dst)
return (NULL);
while (i < len && (start + i) < s_len)
{
dst[i] = s[start + i];
i++;
}
dst[i] = '\0';
return (dst);
}