ft_printf/libft/ft_substr.c
2024-10-15 12:43:54 +02:00

35 lines
1.3 KiB
C

/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* ft_substr.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/10/10 17:00:33 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/10/10 17:00:33 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#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);
}