Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
4.2 kB
0
Indexable
Never
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_split.c                                         :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: cdell <cdell@student.21-school.ru>         +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2021/10/13 23:19:50 by cdell             #+#    #+#             */
/*   Updated: 2021/10/14 20:21:28 by cdell            ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

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

char	*ft_substr(char const *s, unsigned int start, size_t len);
void	ft_bzero(void *s, size_t n);

static int	ft_word_count(char const *s, char c)
{
	int	word_count;
	int	i;

	word_count = 0;
	i = 0;
	while (s[i])
	{
		if (s[i] != c)
		{
			while (s[i] != c && s[i])
				i++;
			word_count++;
		} else
			i++;
	}
	return (word_count);
}

int	ft_word_length(char const *s, char c)
{
	int	n;

	n = 0;
	while (s[n] != c)
		n++;
	return (n);
}

// static void	ft_clean(char **word_array, int i)
// {
// }

char	**ft_split(char const *s, char c)
{
	char	**word_array;
	//char	*word;
	int		word_length;
	int		i;
	int		j;

	word_array = (char **)malloc(sizeof(char) * ft_word_count(s, c) + 1);
	if (word_array == (void *)0)
		return ((void *)0);
	i = 0;
	j = 0;
	while (s[i])
	{
		if (s[i] != c)
		{
			word_length = ft_word_length(s + i, c);
			word_array[j] = ft_substr(s, i, word_length);
			if (word_array[j] == (void *) 0)
			{
				return ((void *) 0);
				// ft_clean(word_array, i);
			}
			//word_array[j] = word;
			i += word_length;
			 int u = 0;
			while (u <= j)
			{
				printf("word = %s\t", word_array[u++]);
			}
			printf("\n");
			j++;

		} else
			i++;
		// printf("i = %d\n", i);
	}
	word_array[j] = (void *) 0;
	return (word_array);
}

// char	**ft_split(char const *s, char c)
// {
// 	char	**word_array;
// 	char	*word;
// 	int		word_length;
// 	int		i;
// 	int		j;
// 	int		k;
// 	int		u;


// 	int word_count = ft_word_count(s, c);
// 	word_array = malloc(sizeof(char) * word_count + 1);
// 	if (word_array == (void *)0)
// 		return ((void *)0);
// 	i = 0;
// 	k = 0;
// 	while (s[i])
// 	{
// 		if (s[i] != c)
// 		{
// 			word_length = ft_word_length(s + i, c);
// 			word = (char *)malloc(sizeof(char) * word_length + 1);
// 			if (word == (void *) 0)
// 				return ((void *) 0);
// 			j = 0;
// 			while (s[i] && s[i] != c)
// 			{
// 				word[j++] = s[i++];
// 				// j++;
// 				// i++;
// 			}
// 			word[j] = '\0';
// 			// printf("k = %d\n", k);
// 			word_array[k] = word;
// 			//ft_bzero(word, word_length);
// 			// printf("word = %s\n", word_array[k]);
// 			k++;
// 			u = 0;
// 			while (u < k)
// 				printf("word = %s\t", word_array[u++]);
// 			printf("\n");
// 		} else
// 			i++;
// 	}
// 	word_array[k] = NULL;
// 	i = 0;
// 	printf("\n");
// 	while (word_array[i] != NULL)
// 	{
// 		printf("word = %s\n", word_array[i]);
// 		i++;
// 	}
// 	return (word_array);
// }

int main()
{
	char delimeter = ' ';
	// int word_count;
	char *s1 = " 444a b6b cc6c dddd eeeee f abc";
	// char *s2 = "123  world!";
	// char *s3 = "1";
	// char *s4 = "  ";
	// // char *s5 = "";
	char **word_array;

	word_array = ft_split(s1, delimeter);
	int i = 0;
	while (word_array[i] != NULL)
	{
		// printf("word = %s\n", word_array[i]);
		i++;
	}

	// word_count = ft_word_count(s1, delimeter);
	// printf("word count = %d\n", word_count);

	// word_count = ft_word_length(s1, delimeter);
	// printf("word length = %d\n", word_count);

	// word_count = ft_word_length(s3, delimeter);
	// printf("word count = %d\n", word_count);

	// word_count = ft_word_length(s4, delimeter);
	// printf("word count = %d\n", word_count);

	// word_count = ft_word_count(s5, delimeter);
	// printf("word count = %d\n", word_count);

	return (0);
}