-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_substr.c
49 lines (45 loc) · 1.63 KB
/
ft_substr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lclerc <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/07 14:24:56 by lclerc #+# #+# */
/* Updated: 2022/11/24 11:00:23 by lclerc ### ########.fr */
/* */
/* ************************************************************************** */
/*
* ft_substr() allocates with malloc() and returns a string of characters from
* string 's'. The new string starts at index 'start' and has as maximum size
* 'len'
*
* ft_substr() returns a pointer to the new string, or NULL if the memory
* allocation has failed.
*
*/
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *new_string;
size_t str_len;
size_t i;
i = 0;
if (!s)
return (NULL);
str_len = ft_strlen(s);
if (start > str_len)
len = 0;
if (ft_strlen(s + start) < len)
len = ft_strlen(s + start);
new_string = (char *)malloc((len + 1) * sizeof(char));
if (!new_string)
return (NULL);
while ((i < len) && s)
{
new_string[i] = s[start + i];
i++;
}
new_string[i] = '\0';
return (new_string);
}