-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_substr.c
More file actions
49 lines (45 loc) · 1.49 KB
/
ft_substr.c
File metadata and controls
49 lines (45 loc) · 1.49 KB
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: sparth <sparth@student.42heilbronn.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/11 10:47:56 by sparth #+# #+# */
/* Updated: 2023/10/20 11:54:12 by sparth ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *substring;
size_t i;
size_t j;
i = 0;
j = 0;
if (len > ft_strlen(s) - start)
len = ft_strlen(s) - start;
if (start >= ft_strlen(s))
return (ft_strdup(""));
substring = malloc((len + 1) * sizeof(char));
if (!s || !substring)
return (NULL);
while (s[i])
{
if (i >= start && j < len)
{
substring[j] = s[i];
j++;
}
i++;
}
substring[j] = '\0';
return (substring);
}
// #include <stdio.h>
// int main ()
// {
// char string [] = "Hello world";
// printf("%s\n", ft_substr(string, 15, 3));
// return (0);
// }