-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_substr.c
44 lines (40 loc) · 1.71 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gcosta-d <gcosta-d@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/25 15:03:45 by gcosta-d #+# #+# */
/* Updated: 2021/09/14 10:54:37 by gcosta-d ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/* Substr return a pointer to a copy string from the big string
* len bytes and counting with the start index.
* If len was 0 or the start index was bigger than the string
* the function have to return a void string.
* If only len was bigger than the string, the copy will return
* all content of the source string.
*/
char *ft_substr(char const *s, unsigned int start, size_t len)
{
size_t s_len;
char *copy;
s_len = ft_strlen(s);
if (start > s_len || len <= 0)
return (ft_strdup(""));
if ((start + len) > s_len)
{
copy = (char *) ft_calloc(s_len - start + 1, sizeof(char));
if (!copy)
return (NULL);
ft_strlcpy(copy, s + start, s_len - start + 1);
return (copy);
}
copy = (char *) ft_calloc(len + 1, sizeof(char));
if (!copy)
return (NULL);
ft_strlcpy(copy, s + start, len + 1);
return (copy);
}