Libft
42 Libft library documentation
Loading...
Searching...
No Matches
ft_substr.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* ft_substr.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: raphalme <raphalme@student.42.fr> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025/12/29 19:02:00 by raphalme #+# #+# */
9/* Updated: 2026/04/14 10:45:22 by raphalme ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "../includes/libft.h"
14
23char *ft_substr(char const *s, unsigned int start, size_t len)
24{
25 char *sub;
26 size_t s_len;
27
28 if (!s)
29 return (NULL);
30 s_len = ft_strlen(s);
31 if (start >= s_len)
32 return (ft_strdup(""));
33 if (len > s_len - start)
34 len = s_len - start;
35 sub = (char *)malloc(sizeof(char) * (len + 1));
36 if (!sub)
37 return (NULL);
38 ft_strlcpy(sub, s + start, len + 1);
39 return (sub);
40}
char * ft_substr(char const *s, unsigned int start, size_t len)
Extracts a substring from a string.
Definition ft_substr.c:23
char * ft_strdup(const char *s1)
Duplicates a C string into newly allocated memory.
Definition ft_strdup.c:21
size_t ft_strlen(const char *str)
Returns the length of a null-terminated string.
Definition ft_strlen.c:21
size_t ft_strlcpy(char *dest, const char *src, size_t size)
Copies a string into a bounded destination buffer.
Definition ft_strlcpy.c:23