Archived
1
0
This repository has been archived on 2024-10-25. You can view files and clone it, but cannot push or open issues or pull requests.
libft/ft_substr.c

39 lines
1.3 KiB
C
Raw Normal View History

2023-11-03 16:21:15 +01:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/02 17:59:58 by adjoly #+# #+# */
2023-11-09 18:28:02 +01:00
/* Updated: 2023/11/09 15:02:46 by adjoly ### ########.fr */
2023-11-03 16:21:15 +01:00
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
size_t i;
char *result;
i = 0;
2023-11-09 18:28:02 +01:00
if (s == NULL)
2023-11-03 16:21:15 +01:00
return (NULL);
2023-11-09 18:28:02 +01:00
if (start > ft_strlen(s))
2023-11-03 16:21:15 +01:00
{
2023-11-09 18:28:02 +01:00
result = calloc(1, 1);
2023-11-03 16:21:15 +01:00
return (result);
}
2023-11-09 18:28:02 +01:00
result = malloc(((ft_strlen(s) - len) + 1) * sizeof(char));
2023-11-03 16:21:15 +01:00
if (result == NULL)
return (NULL);
while (i < len && s[start + i])
{
result[i] = s[start + i];
i++;
}
result[i] = '\0';
return (result);
}