fixe substr quand start commence apres la fin de s
This commit is contained in:
BIN
srcs/part2/.ft_split.c.swp
Normal file
BIN
srcs/part2/.ft_split.c.swp
Normal file
Binary file not shown.
BIN
srcs/part2/a.out
BIN
srcs/part2/a.out
Binary file not shown.
@@ -6,7 +6,7 @@
|
|||||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2019/11/25 14:00:13 by hulamy #+# #+# */
|
/* Created: 2019/11/25 14:00:13 by hulamy #+# #+# */
|
||||||
/* Updated: 2019/11/26 16:18:35 by hulamy ### ########.fr */
|
/* Updated: 2019/11/27 20:43:25 by hulamy ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -14,6 +14,63 @@
|
|||||||
** return an array of string with each word found in str, with c as separator
|
** return an array of string with each word found in str, with c as separator
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
** #include <libc.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);
|
||||||
|
**
|
||||||
|
** //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];
|
||||||
|
** s = "lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. Suspendisse";
|
||||||
|
** // c = av[2][0];
|
||||||
|
** c = ' ';
|
||||||
|
** str = ft_split(s, c);
|
||||||
|
** while (str[i])
|
||||||
|
** printf("%s\n", str[i++]);
|
||||||
|
** // }
|
||||||
|
** return (0);
|
||||||
|
** }
|
||||||
|
*/
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
static int ft_count_word(char const *s, char c)
|
static int ft_count_word(char const *s, char c)
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2019/11/25 14:01:58 by hulamy #+# #+# */
|
/* Created: 2019/11/25 14:01:58 by hulamy #+# #+# */
|
||||||
/* Updated: 2019/11/26 16:18:25 by hulamy ### ########.fr */
|
/* Updated: 2019/11/27 19:54:09 by hulamy ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -40,11 +40,13 @@ char *ft_substr(char const *s, unsigned int start, size_t len)
|
|||||||
|
|
||||||
if (!s)
|
if (!s)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
|
if (ft_strlen(s) < start)
|
||||||
|
return ("");
|
||||||
if (!(str = (char *)malloc(sizeof(char) * (len + 1))))
|
if (!(str = (char *)malloc(sizeof(char) * (len + 1))))
|
||||||
return (NULL);
|
return (NULL);
|
||||||
str[len] = '\0';
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < len)
|
while (i < len && s[start])
|
||||||
str[i++] = s[start++];
|
str[i++] = s[start++];
|
||||||
|
str[i] = '\0';
|
||||||
return (str);
|
return (str);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user