-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_substr.c
71 lines (64 loc) · 1.79 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
68
69
70
71
/*
* ft_substr(): extract substring from string.
*
* DESCRIPTION:
* ============
* Allocates (with malloc(3)) and returns a substring
* from the string ’s’.
* The substring begins at index ’start’ and is of maximum size ’len’.
*/
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
/**
* the lenghth (len) is starting from 1, not from 0.
*/
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *sub_str;
/***
* this if statement to make sure that :
* - we dont make (start) bigger than the length of the string(s).
* - make sure that the length of the substring (sub_str)
*
* the uses of strdup() here is for allocate a memory location
* for the NULL character and return it ('\0').
*/
if (start > strlen(s) || len <= 0)
return (strdup(""));
/**
* Here we are checking if the length of (sub_string) will be more
* the length of main string(s), and if so :
* - use calloc to allocate a specified amount of memory which is (len +1)
* and then initialize it to zero.
*/
if ((start + len) > strlen(s))
{
sub_str = (char *) calloc(strlen(s) - start + 1, sizeof(char));
if (!sub_str)
return (NULL);
/**
* - then copy the main string (s) from the point (start) to the end of the (s).
* # (s + start) ----> pointer to the start of (sub_str)
* # (strlen(s) - start + 1) ----> cause the strlcpy() copy to the (size -1).
*
*/
strlcpy(sub_str, s + start, strlen(s) - start + 1);
return (sub_str);
}
sub_str = (char *) calloc(len + 1, sizeof(char));
if (!sub_str)
return (NULL);
strlcpy(sub_str, s + start, len + 1);
return (sub_str);
}
int main()
{
const char *s;
char *sub_str;
s = "Ghaiath...Abdoush";
sub_str = ft_substr(s, 7, 16);
printf("%lu\n", strlen(s));
printf("%s\n", sub_str);
return EXIT_SUCCESS;
}