-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path03_ft_strjoin.c
129 lines (117 loc) · 2.73 KB
/
03_ft_strjoin.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 03_ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: anajmi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/07/12 21:01:37 by anajmi #+# #+# */
/* Updated: 2021/07/13 21:00:29 by anajmi ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
char *ft_strcat(char *dest, char *src, int add_start)
{
int a;
a = 0;
while (src[a] != '\0')
{
dest[add_start + a] = src[a];
a++;
}
return (dest);
}
int malloc_len(int size, char **strs, char *sep)
{
int malloc_size;
int i;
int j;
malloc_size = 0;
i = 0;
while (i < size)
{
j = 0;
while (strs[i][j] != '\0')
j++;
malloc_size += j;
i++;
}
i = 0;
while (sep[i] != '\0')
i++;
malloc_size += i * (size - 1) + 1;
return (malloc_size);
}
char *join_str_cat(int size, char *string, char **strs, char *sep)
{
int i;
int j;
int add_start;
string[0] = '\0';
i = 0;
add_start = 0;
while (i < size)
{
j = 0;
string = ft_strcat(string, strs[i], add_start);
while (strs[i][j] != '\0')
j++;
add_start += j;
i++;
if (i == size)
break ;
j = 0;
string = ft_strcat(string, sep, add_start);
while (sep[j] != '\0')
j++;
add_start += j;
}
string[add_start] = '\0';
return (string);
}
char *ft_strjoin(int size, char **strs, char *sep)
{
int malloc_size;
char *string;
if (1 <= size)
{
malloc_size = malloc_len(size, strs, sep);
string = (char *)malloc(malloc_size * sizeof(char));
if (!string)
return (NULL);
string = join_str_cat(size, string, strs, sep);
}
else
{
string = (char *)malloc(sizeof(char));
string[0] = '\0';
}
return (string);
}
/*
char *ft_strjoin(int size, char **strs, char *sep)
{
int malloc_size;
char *string;
if (size == 0)
{
string = (char *)malloc(sizeof(char));
string[0] = '\0';
}
else if (1 <= size)
{
malloc_size = malloc_len(size, strs, sep);
string = (char *)malloc(malloc_size * sizeof(char));
string = join_str_cat(size, string, strs, sep);
}
return (string);
}*/
int main(void)
{
char *list[] = {"ACHRAF", "NAJMI", "1337"};
char *sep = "-";
//strcpy(sep, " ");
printf("%s", ft_strjoin(0, list, sep));
return (0);
}