split plus court
This commit is contained in:
@@ -15,13 +15,14 @@
|
||||
*/
|
||||
|
||||
|
||||
#include <libc.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
size_t ft_strlen(const char *str)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
write (1, "gr\n", 3);
|
||||
i = 0;
|
||||
while (str[i])
|
||||
i++;
|
||||
@@ -47,6 +48,7 @@
|
||||
}
|
||||
|
||||
char **ft_split(char const *s, char c);
|
||||
char **ft_strsplit(char const *s, char c);
|
||||
|
||||
//int main(int ac, char **av)
|
||||
int main(void)
|
||||
@@ -62,75 +64,119 @@
|
||||
i = 0;
|
||||
// s = av[1];
|
||||
// c = av[2][0];
|
||||
s = "";
|
||||
s = "ccucuucccuuuccuuuucuuuuuccc";
|
||||
c = 'c';
|
||||
if ((str = ft_split(s, c)))
|
||||
{
|
||||
// if ((str = ft_split(s, c)))
|
||||
// {
|
||||
str = ft_split(s, c);
|
||||
while (str[i])
|
||||
printf("%s\n", str[i++]);
|
||||
}
|
||||
printf("str[%i] : '%s'\n", i, str[i++]);
|
||||
// }
|
||||
// }
|
||||
return (0);
|
||||
}
|
||||
|
||||
//TEST
|
||||
static int ft_cnt_parts(const char *s, char c)
|
||||
{
|
||||
int cnt;
|
||||
int in_substring;
|
||||
|
||||
in_substring = 0;
|
||||
cnt = 0;
|
||||
while (*s != '\0')
|
||||
{
|
||||
if (in_substring == 1 && *s == c)
|
||||
in_substring = 0;
|
||||
if (in_substring == 0 && *s != c)
|
||||
{
|
||||
in_substring = 1;
|
||||
cnt++;
|
||||
}
|
||||
s++;
|
||||
}
|
||||
return (cnt);
|
||||
}
|
||||
|
||||
static int ft_wlen(const char *s, char c)
|
||||
{
|
||||
int len;
|
||||
|
||||
len = 0;
|
||||
while (*s != c && *s != '\0')
|
||||
{
|
||||
len++;
|
||||
s++;
|
||||
}
|
||||
return (len);
|
||||
}
|
||||
|
||||
char **ft_strsplit(char const *s, char c)
|
||||
{
|
||||
char **t;
|
||||
int nb_word;
|
||||
int index;
|
||||
|
||||
index = 0;
|
||||
nb_word = ft_cnt_parts((const char *)s, c);
|
||||
t = (char **)malloc(sizeof(*t) * (ft_cnt_parts((const char *)s, c) + 1));
|
||||
if (t == NULL)
|
||||
return (NULL);
|
||||
while (nb_word--)
|
||||
{
|
||||
while (*s == c && *s != '\0')
|
||||
s++;
|
||||
t[index] = ft_substr((const char *)s, 0, ft_wlen((const char *)s, c));
|
||||
if (t[index] == NULL)
|
||||
return (NULL);
|
||||
s = s + ft_wlen(s, c);
|
||||
index++;
|
||||
}
|
||||
t[index] = NULL;
|
||||
return (t);
|
||||
}
|
||||
//TESTFIN
|
||||
|
||||
//#include "libft.h"
|
||||
|
||||
static int ft_count_word(char const *s, char c)
|
||||
static int count(char const *s, char c)
|
||||
{
|
||||
int i;
|
||||
int words;
|
||||
|
||||
write (1, "gt\n", 3);
|
||||
i = 0;
|
||||
i = -1;
|
||||
words = 0;
|
||||
while (s[i])
|
||||
{
|
||||
if (s[i] != c)
|
||||
{
|
||||
words++;
|
||||
while (s[i] && s[i] != c)
|
||||
while (s[++i])
|
||||
if (s[i] != c && ++words)
|
||||
while (s[i + 1] && s[i + 1] != c)
|
||||
i++;
|
||||
}
|
||||
else
|
||||
i++;
|
||||
}
|
||||
return (words);
|
||||
}
|
||||
|
||||
void *ft_free(char **array)
|
||||
{
|
||||
while (array)
|
||||
free(array++);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
char **ft_split(char const *s, char c)
|
||||
{
|
||||
char **array;
|
||||
int i;
|
||||
int j;
|
||||
int w;
|
||||
int len;
|
||||
|
||||
write (1, "gu\n", 3);
|
||||
if (!s || !c || !(i = ft_count_word(s, c)))
|
||||
w = 0;
|
||||
if (!s || !c || !(array = malloc(sizeof(char *) * (count(s, c) + 1))))
|
||||
return (NULL);
|
||||
if (!(array = malloc(sizeof(char *) * (i + 1))))
|
||||
return (NULL);
|
||||
array[i] = 0;
|
||||
i = -1;
|
||||
j = 0;
|
||||
while (s[++i])
|
||||
while (*s && !(len = 0))
|
||||
{
|
||||
if (s[i] != c)
|
||||
if (*s != c)
|
||||
{
|
||||
len = 0;
|
||||
while (s[i + len] && s[i + len] != c)
|
||||
while (s[len] && s[len] != c)
|
||||
len++;
|
||||
if (!(array[j++] = ft_substr(s, i, len)))
|
||||
return (ft_free(array));
|
||||
i = i + len - 1;
|
||||
if (!(array[w++] = ft_substr(s, 0, len)))
|
||||
{
|
||||
while (array)
|
||||
free(array++);
|
||||
return (NULL);
|
||||
}
|
||||
}
|
||||
s += len ? len : 1;
|
||||
}
|
||||
array[w] = NULL;
|
||||
return (array);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user