Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "libft.h"
- #include <stdio.h>
- /*
- ** ft_strntok - extract tokens from strings
- **
- ** <const char *str> input string
- ** <const char *delim> delimiter set
- ** <int count> delimiter count, takes string before delimiter
- **
- ** Parses a string into a sequence of tokens.
- ** Splits based on delimiter(s) and returns sequence
- **
- ** Similar to MS-DOS FOR /F "tokens=* delims=*"
- */
- char *ft_strntok(const char *str, const char *delim, int count)
- {
- char *tokens;
- int index = 0;
- tokens = (char *)ft_calloc(1, ft_strlen(str) + 1);
- while (*str != '\0' && count > 0)
- {
- while (ft_strchr(delim, *str) == NULL)
- {
- tokens[index] = *str;
- str++;
- index++;
- }
- if (count == 0 || *str == '\0')
- return (tokens);
- while (ft_strchr(delim, *str) != NULL)
- {
- str++;
- if (ft_strchr(delim, *str) == NULL)
- return (ft_strntok(str, delim, count - 1));
- }
- tokens++;
- str++;
- }
- return (NULL);
- }
- int main(void)
- {
- printf("[%s]", ft_strntok("SPLIT THIS FUCKING STRING", ' ', 2));
- // 0 returns SPLIT
- // 1 returns THIS
- // 2 returns FUCKING
- // 3 returns STRING
- }
Advertisement
Add Comment
Please, Sign In to add comment