Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ************************************************************************** */
- /* */
- /* ::: :::::::: */
- /* ft_split.c :+: :+: :+: */
- /* +:+ +:+ +:+ */
- /* By: marvin <marvin@student.42.fr> +#+ +:+ +#+ */
- /* +#+#+#+#+#+ +#+ */
- /* Created: 2019/11/11 23:11:24 by marvin #+# #+# */
- /* Updated: 2019/11/11 23:11:24 by marvin ### ########.fr */
- /* */
- /* ************************************************************************** */
- #include <stdlib.h>
- #include <stdio.h>
- void ft_free(char **dest, int wcount)
- {
- while (wcount >= 0)
- {
- free(dest[wcount]);
- wcount--;
- }
- free(dest);
- }
- int ft_countletter(char *s, char c, int i)
- {
- int j;
- j = 0;
- while (s[i + j] != c)
- j++;
- return (j);
- }
- char **ft_fillword(char *s, char c, char **dest)
- {
- int i;
- int letter;
- int wcount;
- i = 0;
- wcount = 0;
- while (s[i])
- {
- while (s[i] == c)
- i++;
- if (s[i] == 0)
- return (dest);
- if (!(dest[wcount] = malloc((ft_countletter(s, c, i) + 1) * sizeof(char))))
- ft_free(dest, wcount);
- letter = 0;
- while (s[i] != c && s[i] != 0)
- {
- dest[wcount][letter] = s[i];
- i++;
- letter++;
- }
- dest[wcount][letter] = 0;
- wcount++;
- }
- dest[wcount] = 0;
- return (dest);
- }
- int ft_countworld(char *s, char c)
- {
- int i;
- int wcount;
- i = 0;
- wcount = 0;
- while (s[i])
- {
- while (s[i] == c)
- i++;
- if (s[i] == 0)
- return (wcount);
- wcount++;
- while (s[i] != c && s[i] != 0)
- i++;
- }
- return (wcount);
- }
- char **ft_split(char const *s, char c)
- {
- char **dest;
- int w_count;
- w_count = ft_countworld((char *)s, c);
- if (!w_count)
- return (0);
- if (!(dest = malloc((w_count + 1) * sizeof(char *))))
- return (0);
- dest = ft_fillword((char *)s, c, dest);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement