ft_substr
Subject
FT_SUBSTR (simplified)
NAME
ft_substr -- extract a substring from a string
SYNOPSIS
char *ft_substr(const char *s, unsigned int start, size_t len);
DESCRIPTION
Allocate (with malloc(3)) and return a new string from the string s.
This new string starts at index 'start' and has a maximum size of 'len'.
PARAMETERS
s: string from which to extract the new string
start: start index of the new string in the string 's'
len: maximum size of the new string
RETURN VALUES
ft_substr() returns the new string; NULL if the memory allocation failed.
AUTHORIZED EXTERNAL FUNCTIONS
malloc(3)Understandable explanation
ft_substr returns a substring of the string s passed as parameter.
Here's an example
ft_substr("Bonjour comment ca va?", 5, 8);
=> "ur comme"Hints
First we have to check if the start index is greater than the length of the string or not.
We also have to check if the start plus the len is greater than the length of the whole string.
Then we allocate enough memory for the substring, and copy from s[start] until we reach len characters copied into our new string.
And finally we can return the substring.
Commented solution
Last updated
Was this helpful?