42_libft/ft_substr.c

36 lines
1.3 KiB
C
Raw Permalink Normal View History

2022-09-27 11:25:41 -04:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
2022-10-04 08:21:55 -04:00
/* ft_substr.c :+: :+: :+: */
2022-09-27 11:25:41 -04:00
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
2022-10-04 08:21:55 -04:00
/* Created: 2022/09/28 18:53:44 by cchauvet #+# #+# */
2022-10-05 15:04:45 -04:00
/* Updated: 2022/10/05 20:53:19 by cchauvet ### ########.fr */
2022-09-27 11:25:41 -04:00
/* */
/* ************************************************************************** */
#include "libft.h"
2022-10-05 15:04:45 -04:00
char *ft_substr(char const *s, unsigned int start, size_t len)
2022-09-27 11:25:41 -04:00
{
2022-10-05 15:04:45 -04:00
ssize_t size;
char *ptr;
2022-09-27 11:25:41 -04:00
2022-10-05 15:04:45 -04:00
if (s == NULL)
2022-10-04 08:21:55 -04:00
return (NULL);
2022-10-05 15:04:45 -04:00
size = ft_strlen(s);
size -= start;
if (size < 0)
size = 0;
if ((size_t)size > len)
size = len;
ptr = malloc((size + 1) * sizeof(char));
if (ptr == NULL)
2022-10-04 08:21:55 -04:00
return (NULL);
2022-10-05 15:04:45 -04:00
ptr[size] = '\0';
while (size-- > 0)
ptr[size] = s[start + size];
return (ptr);
2022-09-27 11:25:41 -04:00
}