Skip to content

Instantly share code, notes, and snippets.

@yclim95
Last active June 6, 2022 00:57
Show Gist options
  • Save yclim95/e07d56a60fdbef3f1a2899ccae0fd9d0 to your computer and use it in GitHub Desktop.
Save yclim95/e07d56a60fdbef3f1a2899ccae0fd9d0 to your computer and use it in GitHub Desktop.
ft_split() in c
#include <stdio.h>
#include <stdlib.h>

static size_t	ft_word_count(char const *s, char c)
{
	size_t count;

	count = 0;
	/*
	while (*s && *s == c)
		s++;
	*/
	while (*s)
	{
		while (*s && *s != c) 
			s++;
		while (*s && *s == c)
		    s++;
		
		count++;
	}
	return (count);
}

static size_t	ft_word_len(char const *s, char c)
{
	size_t len;

	len = 0;
	//printf("s (wordlen): %s\n", s);
	while (*s != c && *s)
	{
		len++;
		s++;
	}
	return (len);
}

static char		*ft_strndup(const char *s, size_t n)
{
	char	*pt_strndup;
	size_t	size;
	
	pt_strndup = malloc(sizeof(char) * (n + 1));
	if (!pt_strndup)
		return (NULL);
	size = 0;
	while (size < n)
	{
		pt_strndup[size] = s[size];
		size++;
	}
	pt_strndup[size] = '\0';
	return (pt_strndup);
}

static void		ft_arr_free(char **s, int i)
{
	while (i--)
		free(s[i]);
	free(s);
}

char			**ft_split(char const *s, char c)
{
	char	**pt_split;
	size_t	len;
	size_t	wordlen;
	size_t	i;

	len = ft_word_count(s, c); // Count how many word needed to reserve memory space
	pt_split = malloc(sizeof(char *) * (len + 1));
	if (!pt_split)
		return (NULL);
	i = 0;
	while (i < len)
	{
		while (*s && *s == c) // Check how many characters == separator
		{
			s++;
		//printf("s: %s\n", s);
		}
		wordlen = ft_word_len(s, c); // len of word (separated by c)
		pt_split[i] = ft_strndup(s, wordlen); // Duplicate s[wordlen] to pt_split[i]
		printf("i: %ld\n", i);
		printf("s: %s\n", pt_split[i]);
		printf("s: %s\n", pt_split[15]);
		if (!pt_split[i]) // If pt_split[i] does not exist (free current memory space)
		{
			ft_arr_free(pt_split, i);
			return (NULL);
		}
		s += wordlen; // ExL '42 KL' s + (2) = ' KL'
		i++;
	}
	pt_split[len] = '\0';
	return (pt_split);
}

int main()
{
 	char *str = "42 KL is awesome";
 	char **result = ft_split(str, ' ');
 	printf("The result is: %s\n", *result);
 	printf("The result is: %s\n", result[0]);
 	printf("The result is: %s\n", result[1]);
 	printf("The result is: %s\n", result[2]);
 	printf("The result is: %s\n", result[3]);
 	return 0;
}
i: 0
s: 42
s: (null)
i: 1
s: KL
s: (null)
i: 2
s: is
s: (null)
i: 3
s: awesome
s: (null)
The result is: 42
The result is: 42
The result is: KL
The result is: is
The result is: awesome

Updates

#include <stdio.h>
#include <stdlib.h>

static size_t	ft_word_count(char const *s, char c)
{
	size_t count;

	count = 0;
	/*
	while (*s && *s == c)
		s++;
	*/
	while (*s)
	{
		while (*s && *s != c) 
			s++;
		while (*s && *s == c)
		    s++;
		
		count++;
	}
	return (count);
}

static size_t	ft_word_len(char const *s, char c)
{
	size_t len;

	len = 0;
	//printf("s (wordlen): %s\n", s);
	while (*s != c && *s)
	{
		len++;
		s++;
	}
	return (len);
}

static char		*ft_strndup(const char *s, size_t n)
{
	char	*pt_strndup;
	size_t	size;
	
	pt_strndup = malloc(sizeof(char) * (n + 1));
	if (!pt_strndup)
		return (NULL);
	size = 0;
	while (size < n)
	{
		pt_strndup[size] = s[size];
		size++;
	}
	pt_strndup[size] = '\0';
	return (pt_strndup);
}

static void		ft_arr_free(char **s, int i)
{
	while (i--)
		free(s[i]);
	free(s);
}

char			**ft_split(char const *s, char c)
{
	char	**pt_split;
	size_t	len;
	size_t	wordlen;
	size_t	i;

	len = ft_word_count(s, c); // Count how many word needed to reserve memory space
	pt_split = malloc(sizeof(char *) * (len + 1));
	if (!pt_split)
		return (NULL);
	i = 0;
	while (i < len)
	{
		while (*s && *s == c) // Check how many characters == separator
		{
			s++;
		//printf("s: %s\n", s);
		}
		wordlen = ft_word_len(s, c); // len of word (separated by c)
		pt_split[i] = ft_strndup(s, wordlen); // Duplicate s[wordlen] to pt_split[i]
		printf("i: %ld\n", i);
		printf("s: %s\n", pt_split[i]);
		printf("s: %s\n", pt_split[15]);
	    if (!pt_split[i]) // If pt_split[i] does not exist (free current memory space)
	    {
		    ft_arr_free(pt_split, i);
		    //return (NULL);
	    }
		s += wordlen; // ExL '42 KL' s + (2) = ' KL'
		i++;
	}
	pt_split[len] = '\0';
	return (pt_split);
}

int main()
{
 	char *str = "42 KL is awesome";
 	char **result = ft_split(str, '2');
 	//printf("The result is: %s\n", *result);
 	printf("The result is: %s\n", result[0]);
 	printf("The result is: %s\n", result[1]);
 	printf("The result is: %s\n", result[2]);
 	printf("The result is: %s\n", result[3]);
 	return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment