-
Notifications
You must be signed in to change notification settings - Fork 0
/
libft.c
145 lines (126 loc) · 2.8 KB
/
libft.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <stdio.h>
#include <stdlib.h>
size_t ft_strlen(const char *s){
size_t count = 0;
while (*s++) count++;
return count;
}
int ft_atoi(const char *str){
int sign = 1, ans = 0;
if (str[0] == '-'){
sign *= -1;
str++;
}
while (*str){
ans = ans * 10 + *str - '0';
str++;
}
return ans * sign;
}
int ft_islower(const char c){
return ('a' <= c && c <= 'z');
}
int ft_isupper(const char c){
return ('A' <= c && c <= 'Z');
}
int ft_isalpha(const char c){
return (ft_isupper(c) || ft_islower(c));
}
int ft_isascii(const char c){
return (0 <= c && c <= 127);
}
int ft_isdigit(const char c){
return ('0' <= c && c <= '9');
}
int ft_isspace(const char c){
return (c == ' ');
}
char ft_tolower(char c){
if ('A' <= c && c <= 'Z') c += 32;
return c;
}
char ft_toupper(char c){
if ('a' <= c && c <= 'z') c -= 32;
return c;
}
static size_t ft_intlen(int n){
int len = 0;
if (n < 0){
len++;
n *= -1;
}
while (n > 0){
n /= 10;
len++;
}
return len;
}
char *ft_itoa(int n){
size_t len = ft_intlen(n);
char *str = malloc(sizeof(char) * len + 1);
str[len] = '\0';
if (n < 0){
str[0] = '-';
n *= -1;
len--;
}
while (n > 0){
str[len] = n % 10 + 48;
n /= 10;
len--;
}
return str;
}
static size_t ft_count_words(const char *str, const char c){
size_t count = 1;
while (*str){
if (*str++ == c) count++;
}
return count;
}
static void ft_write_words(const char *str, const char c, int nb_words, char **ans){
int i = -1, j = 0, k;
while (++i < nb_words){
k = 0;
while (str[j] == c) j++;
while (str[j] && str[j] != c){
ans[i][k++] = str[j++];
}
ans[i][k] = '\0';
}
}
static void ft_malloc_words(const char *str, const char c, int nb_words, char **ans){
int i = -1, j = 0, k;
while (++i < nb_words){
k = 0;
while (str[j] == c) j++;
while (str[j] && str[j] != c){
j++;
k++;
}
ans[i] = (char *)malloc(sizeof(char) * (k + 1));
}
}
char **ft_split(const char *str, const char c){
size_t nb_words = ft_count_words(str, c);
char **ans = malloc(sizeof(char *) * (nb_words + 1));
ft_malloc_words(str, c, nb_words, ans);
ft_write_words(str, c, nb_words, ans);
ans[nb_words] = 0;
return ans;
}
char *ft_substr(const char *str, const int start, const int num){
char *ans = malloc(sizeof(char) * num + 1);
int i = start;
while (i < start + num && str[i]){
*(ans+i-start) = *(str+i);
i++;
}
*(ans+num) = '\0';
return ans;
}
int main(){
char *s = ft_substr("Heyksdo", 4, 3);
printf("%s", s);
return 0;
}