Libft
42 Libft library documentation
Loading...
Searching...
No Matches
ft_substr.c File Reference
+ Include dependency graph for ft_substr.c:

Go to the source code of this file.

Functions

char * ft_substr (char const *s, unsigned int start, size_t len)
 Extracts a substring from a string.
 

Function Documentation

◆ ft_substr()

char * ft_substr ( char const *  s,
unsigned int  start,
size_t  len 
)

Extracts a substring from a string.

Parameters
sSource string.
startStart index in s.
lenMaximum substring length.
Returns
Newly allocated substring, or NULL on failure.

Definition at line 23 of file ft_substr.c.

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_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

References ft_strdup(), ft_strlcpy(), and ft_strlen().

Referenced by ft_fill_split(), and ft_strtrim().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: