You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.3 KiB
43 lines
1.3 KiB
3 years ago
|
/* ************************************************************************** */
|
||
|
/* */
|
||
|
/* ::: :::::::: */
|
||
|
/* ft_substr.c :+: :+: :+: */
|
||
|
/* +:+ +:+ +:+ */
|
||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
|
||
|
/* +#+#+#+#+#+ +#+ */
|
||
|
/* Created: 2021/10/20 06:45:32 by narnaud #+# #+# */
|
||
|
/* Updated: 2021/11/15 11:12:02 by narnaud ### ########.fr */
|
||
|
/* */
|
||
|
/* ************************************************************************** */
|
||
|
|
||
|
#include "libft.h"
|
||
|
|
||
|
char *ft_substr(char const *s, unsigned int start, size_t len)
|
||
|
{
|
||
|
size_t i;
|
||
|
char *ret;
|
||
|
size_t s_size;
|
||
|
|
||
|
if (!s)
|
||
|
return (NULL);
|
||
|
s_size = ft_strlen(s);
|
||
|
if (start > s_size)
|
||
|
{
|
||
|
start = s_size;
|
||
|
len = 0;
|
||
|
}
|
||
|
else if (s_size < start + len)
|
||
|
len = s_size - start;
|
||
|
ret = ft_calloc(len + 1, sizeof(char));
|
||
|
if (!ret)
|
||
|
return (NULL);
|
||
|
i = 0;
|
||
|
while (i < len && s[start + i])
|
||
|
{
|
||
|
ret[i] = s[start + i];
|
||
|
i++;
|
||
|
}
|
||
|
ret[i] = '\0';
|
||
|
return (ret);
|
||
|
}
|