-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathft_substr.c
60 lines (51 loc) · 1.61 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
55
56
57
58
59
60
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jede-ara <jede-ara@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/08 14:24:08 by jede-ara #+# #+# */
/* Updated: 2022/11/08 14:24:44 by jede-ara ### ########.fr */
/* */
/* ************************************************************************** */
/*
DESCRIÇÃO: substr() extrai a substring da string, aloca (com malloc(3)) e
retorna uma substring da string 's'. A substring começa no índice 'start'
e tem tamanho máximo 'len'.
*/
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *str;
size_t i;
size_t j;
if (!s)
return (NULL);
str = (char *)malloc(sizeof(*s) * (len + 1));
if (!str)
return (0);
i = 0;
j = 0;
while (s[i])
{
if (i >= start && j < len)
{
str[j] = s[i];
j++;
}
i++;
}
str[j] = 0;
return (str);
}
/*#include <stdio.h>
int main()
{
char s[] = "Extraindo uma substring de s";
int start = 10;
int len = 30;
char* str = ft_substr(s, start, len);
printf("%s\n", str);
return (0);
}*/