split reparee

This commit is contained in:
Hugo LAMY
2019-12-04 15:51:58 +01:00
parent bc40b73a9a
commit dc0a5d69d9
3 changed files with 62 additions and 127 deletions

View File

@@ -5,8 +5,8 @@
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:00:13 by hulamy #+# #+# */
/* Updated: 2019/12/03 19:07:08 by hulamy ### ########.fr */
/* Created: 2019/12/04 15:51:26 by hulamy #+# #+# */
/* Updated: 2019/12/04 15:51:28 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,130 +14,65 @@
** return an array of string with each word found in str, with c as separator
*/
/*
** #include <stdio.h>
** #include <stdlib.h>
** #include <unistd.h>
**
** size_t ft_strlen(const char *str)
** {
** size_t i;
**
** i = 0;
** while (str[i])
** i++;
** return (i);
** }
**
** char *ft_substr(char const *s, unsigned int start, size_t len)
** {
** char *str;
** size_t i;
**
** if (!s)
** return (NULL);
** if (ft_strlen(s) < start)
** return ("");
** if (!(str = (char *)malloc(sizeof(char) * (len + 1))))
** return (NULL);
** i = 0;
** while (i < len && s[start])
** str[i++] = s[start++];
** str[i] = '\0';
** return (str);
** }
**
** char **ft_split(char const *s, char c);
** char **ft_strsplit(char const *s, char c);
**
** int main(void)
** {
** char **str;
** int i;
**
** char *s;
** char c;
**
** i = -1;
** s = NULL;
** c = 'c';
** str = ft_split(s, c);
** if (str)
** {
** printf("*str : '%p'\n", str[0]);
** while (str[++i])
** printf("str[%i] : '%s'\n", i, str[i]);
** }
** return (0);
** }
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
size_t ft_strlen(const char *str)
{
size_t i;
i = 0;
while (str[i])
i++;
return (i);
}
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *str;
size_t i;
if (!s)
return (NULL);
if (ft_strlen(s) < start)
return ("");
if (!(str = (char *)malloc(sizeof(char) * (len + 1))))
return (NULL);
i = 0;
while (i < len && s[start])
str[i++] = s[start++];
str[i] = '\0';
return (str);
}
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)
{
char **str;
int i;
char *s;
char c;
// if (ac == 3)
// {
i = 0;
// s = av[1];
// c = av[2][0];
s = "ccucuucccuuuccuuuucuuuuuccc";
c = 'c';
// if ((str = ft_split(s, c)))
// {
str = ft_split(s, c);
while (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"
#include "libft.h"
static int count(char const *s, char c)
{