Cub3D/ParsingCub3D/libft/ft_substr.c
2022-06-07 20:15:32 +02:00

39 lines
1.3 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sadjigui <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/06/03 12:11:04 by sadjigui #+# #+# */
/* Updated: 2021/06/07 11:33:45 by sadjigui ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *str;
size_t i;
size_t j;
i = -1;
if (!s)
return (NULL);
j = ft_strlen(s);
if (!len || j <= start)
return (ft_strdup(""));
str = (char *)malloc(sizeof(char) * len + 1);
if (!str)
return (NULL);
j = 0;
while (s[++i] && j < len)
{
if (i >= start)
str[j++] = s[i];
}
str[j] = '\0';
return (str);
}