-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_substr.c
41 lines (38 loc) · 1.49 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: estettle <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/03 10:10:20 by estettle #+# #+# */
/* Updated: 2024/10/03 10:52:59 by estettle ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/* ft_substr()
* allocates a substring from the string s, starting from index start, that is
* len characters long, then returns it
*/
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *substring;
size_t i;
if (start >= ft_strlen(s) + 1)
return (ft_calloc(1, sizeof(char)));
if (ft_strlen(s) - start < len)
substring = ft_calloc(ft_strlen(s) - start + 1, sizeof(char));
else
substring = ft_calloc(len + 1, sizeof(char));
if (!substring)
return (NULL);
i = 0;
s += start;
while (i < len && s[i])
{
substring[i] = s[i];
i++;
}
substring[i] = '\0';
return (substring);
}