libft
Loading...
Searching...
No Matches
Functions
ft_split.c File Reference

Splits a string into substrings using a single delimiter character. More...

#include "libft.h"
+ Include dependency graph for ft_split.c:

Functions

static int ft_fill_array (char **array, const char *s, char c, size_t w_count)
 Fills the output array with substrings from input.
 
char ** ft_split (const char *s, char c)
 Splits a string into substrings based on a delimiter character.
 
static size_t ft_w_count (const char *str, char c)
 Counts how many substrings will result from splitting.
 

Detailed Description

Splits a string into substrings using a single delimiter character.

Author
Toonsa
Date
2025/04/05

This file implements the ft_split function, which breaks a string into an array of substrings based on a single delimiter character. Substrings are dynamically allocated, and the output is a NULL-terminated array. Internal helpers handle word counting and array filling.

Function Documentation

◆ ft_fill_array()

static int ft_fill_array ( char **  array,
const char *  s,
char  c,
size_t  w_count 
)
static

Fills the output array with substrings from input.

Allocates substrings from s using ft_substr and stores them in the provided array. The array is NULL-terminated. On allocation failure, previously allocated substrings are freed.

Parameters
arrayThe output array of substrings.
sThe input string to split.
cThe delimiter character.
w_countThe number of substrings to extract.
Returns
1 on success, 0 on failure.
See also
ft_substr
ft_free_array

◆ ft_w_count()

static size_t ft_w_count ( const char *  str,
char  c 
)
static

Counts how many substrings will result from splitting.

Traverses the input string and counts how many substrings (non-empty sequences not interrupted by the delimiter) will be generated.

Parameters
strThe input string to scan.
cThe delimiter character.
Returns
The number of substrings (words) in the input.