-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_substr.c
67 lines (61 loc) · 1.7 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
61
62
63
64
65
66
67
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kgriset <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/11 11:25:12 by kgriset #+# #+# */
/* Updated: 2024/01/30 12:01:05 by kgriset ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *create_sub(const char *s, char *sub, unsigned int start,
size_t len)
{
size_t i;
i = 0;
while (s[start + i] && i != len)
{
sub[i] = s[start + i];
i++;
}
sub[i] = '\0';
return (sub);
}
char *ft_substr(char const *s, unsigned int start, size_t len)
{
size_t i;
char *sub;
size_t l_s;
i = 0;
l_s = ft_strlen(s);
if (start > l_s)
{
sub = malloc(sizeof(char));
if (!sub)
return (NULL);
*sub = '\0';
return (sub);
}
while (s[start + i] && i != len)
i++;
sub = malloc(i * sizeof(char) + sizeof(char));
if (!sub)
return (NULL);
else
{
sub = create_sub(s, sub, start, len);
return (sub);
}
}
//#include <stdio.h>
//#include <stdlib.h>
// int main ()
//{
// char *str = "Hello 42";
// char *sub = ft_substr(str, 0, 5);
// printf("%s\n",sub);
// free(sub);
// return (1);
//}