ft_split — Split String into Words
Allowed functions: malloc
Write a function that takes a string, splits it into words, and returns them as a NULL-terminated array of strings.
A "word" is defined as a part of a string delimited either by spaces/tabs/new lines, or by the start/end of the string.
char **ft_split(char *str);
Solution
Download ft_split.c#include <stdlib.h>
int sep(char c) { return (c == ' ' || c == '\t' || c == '\n'); }
int cntwords(char *str) {
int count;
count = 0;
while (*str) {
while (*str && sep(*str))
str++;
if (*str) {
count++;
while (*str && !sep(*str))
str++;
}
}
return (count);
}
char *getword(char *str, int start, int length) {
char *word;
int i;
word = malloc(sizeof(char) * (length + 1));
i = 0;
while (i < length) {
word[i] = str[start + i];
i++;
}
word[i] = '\0';
return (word);
}
char **ft_split(char *str) {
char **newstr;
int i;
int k;
int start;
newstr = malloc(sizeof(char *) * (cntwords(str) + 1));
i = 0;
k = 0;
while (str[i]) {
while (str[i] && sep(str[i]))
i++;
if (str[i]) {
start = i;
while (str[i] && !sep(str[i]))
i++;
newstr[k++] = getword(str, start, i - start);
}
}
newstr[k] = NULL;
return (newstr);
}
How It Works
Goal: Split a string into an array of words, separated by spaces, tabs, or newlines.
Approach: Count words first to allocate the array, then iterate again to extract each word into its own allocated string.
Step by step:
sepchecks if a character is a whitespace delimiter (space, tab, or newline).cntwordswalks the string, skipping separators and counting each group of non-separator characters as one word.- Allocate a
char **array with room for all words plus a NULL terminator. - Walk the string again: skip separators, record the start index, advance to the end of the word, then call
getwordtomallocand copy that substring. - Terminate the array with
NULL.
Key concept: String tokenization with two-pass logic and 2D dynamic memory allocation (an array of individually allocated strings).