42_libft/ft_substr.c

35 lines
1.3 KiB
C
Raw 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-04 16:04:23 -04:00
/* Updated: 2022/10/04 19:16:16 by cchauvet ### ########.fr */
2022-09-27 11:25:41 -04:00
/* */
/* ************************************************************************** */
#include "libft.h"
2022-10-04 08:21:55 -04:00
char *ft_substr(const char *s, unsigned int start, size_t len)
2022-09-27 11:25:41 -04:00
{
size_t max;
2022-10-04 08:21:55 -04:00
char *sub;
2022-09-27 11:25:41 -04:00
2022-10-04 08:21:55 -04:00
if (s == NULL || !*s)
return (NULL);
max = ft_strlen(s);
if (max > len)
max = len;
if (start >= ft_strlen(s))
return (ft_strdup(""));
2022-10-04 16:04:23 -04:00
sub = malloc((max + 1) * sizeof(char));
2022-10-04 08:21:55 -04:00
if (sub == NULL)
return (NULL);
2022-10-04 16:04:23 -04:00
sub[max] = 0;
while (max-- > 0)
sub[max] = s[max + start];
2022-10-04 08:21:55 -04:00
return (sub);
2022-09-27 11:25:41 -04:00
}