39 lines
1.4 KiB
C
39 lines
1.4 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* ft_substr.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: grobledo <marvin@42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2023/02/14 14:52:48 by grobledo #+# #+# */
|
|
/* Updated: 2023/02/14 14:52:51 by grobledo ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
#include "libft.h"
|
|
|
|
char *ft_substr(const char *s, unsigned int start, size_t len)
|
|
{
|
|
size_t i;
|
|
size_t j;
|
|
char *dest;
|
|
|
|
if (!s)
|
|
return (NULL);
|
|
i = start;
|
|
j = 0;
|
|
if (start >= ft_strlen(s))
|
|
return (ft_calloc(1, 1));
|
|
if (ft_strlen(s + start) < len)
|
|
dest = (char *)ft_calloc((ft_strlen(s + start) + 1), sizeof(char));
|
|
else
|
|
dest = (char *)ft_calloc((len + 1), sizeof(char));
|
|
if (dest == 0)
|
|
return (NULL);
|
|
while (j < len && s[i])
|
|
{
|
|
dest[j] = s[i];
|
|
i++;
|
|
j++;
|
|
}
|
|
return (dest);
|
|
}
|