-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_substr.c
54 lines (48 loc) · 1.59 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
50
51
52
53
54
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lpicoli- < lpicoli-@student.42porto.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/14 17:23:00 by lpicoli- #+# #+# */
/* Updated: 2022/11/23 10:52:08 by lpicoli- ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
** Allocates (with malloc(3)) and returns a substring
** from the string ’s’. The substring begins at index
** ’start’ and is of maximum size ’len’.
*/
char *ft_substr(char const *s, unsigned int start, size_t len)
{
size_t i;
size_t j;
char *result_str;
i = 0;
j = 0;
result_str = malloc(sizeof(char) * (len + 1));
if (!result_str || !s)
return (NULL);
while (s[i])
{
while (i >= start && j < len)
{
result_str[j] = s[i];
j++;
i++;
}
i++;
}
result_str[j] = '\0';
return (result_str);
}
/* int main()
{
char *s = "lorem ipsum dolor sit amet";
unsigned int start = 0;
size_t len = 5;
char *substr = (ft_substr(s, start, len));
puts(substr);
} */