48 lines
1.4 KiB
C
48 lines
1.4 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* ft_substr.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: madumerg <madumerg@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2023/11/06 11:14:21 by madumerg #+# #+# */
|
|
/* Updated: 2023/11/09 16:27:51 by madumerg ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "libft.h"
|
|
|
|
int ft_min(int lenb, int len)
|
|
{
|
|
if (len < lenb)
|
|
return (len);
|
|
return (lenb);
|
|
}
|
|
|
|
char *ft_substr(char const *s, unsigned int start, size_t len)
|
|
{
|
|
char *str;
|
|
size_t lenb;
|
|
size_t i;
|
|
size_t j;
|
|
|
|
if (!s)
|
|
return (NULL);
|
|
if (start > ft_strlen(s))
|
|
return (ft_calloc(1, 1));
|
|
lenb = ft_strlen(s) - start;
|
|
str = malloc(sizeof(char) * ft_min(lenb, len) + 1);
|
|
if (!str)
|
|
return (NULL);
|
|
i = start;
|
|
j = 0;
|
|
while (s[i] && j < len)
|
|
{
|
|
str[j] = s[i];
|
|
i++;
|
|
j++;
|
|
}
|
|
str[j] = '\0';
|
|
return (str);
|
|
}
|