libft_old/extended_libft/ft_substr.c
Willem Haffmans 326f52308e all
2024-07-26 22:32:04 +02:00

35 lines
1.3 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:11 by whaffman #+# #+# */
/* Updated: 2024/07/10 12:27:02 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *substr;
size_t len_s;
if (!s)
return (NULL);
len_s = ft_strlen(s);
if (start >= len_s)
return (ft_strdup(""));
if (len_s - start < len)
len = len_s - start;
substr = malloc(len * sizeof(char) + 1);
if (!substr)
return (NULL);
ft_memcpy(substr, &s[start], len);
substr[len] = '\0';
return (substr);
}