libft: add functions to get the first character matching or not matching a predicate

This commit is contained in:
Khaïs COLIN 2025-02-14 15:06:01 +01:00
parent 8defbf4d13
commit ac10c3b4de
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
4 changed files with 67 additions and 1 deletions

View file

@ -6,7 +6,7 @@
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 16:06:09 by jguelen #+# #+# */
/* Updated: 2025/02/20 14:48:28 by khais ### ########.fr */
/* Updated: 2025/02/20 14:59:22 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,6 +14,7 @@
# define LIBFT_H
# include <unistd.h>
# include <ctype.h>
# include <stdbool.h>
# include <string.h>
# include <strings.h>
# include <stdlib.h>
@ -132,6 +133,8 @@ int ft_tolower(int c);
char *ft_strchr(const char *s, int c);
size_t ft_strchridx(const char *s, char c);
size_t ft_strnchridx(const char *s, char c);
size_t ft_strfchridx(const char *s, bool f(char));
size_t ft_strnfchridx(const char *s, bool f(char));
char *ft_strrchr(const char *s, int c);
int ft_strcmp(const char *s1, const char *s2);
int ft_strncmp(const char *s1, const char *s2, size_t n);

View file

@ -48,6 +48,7 @@ SOURCES = \
ft_strmapi.c \
ft_strnchridx.c \
ft_strncmp.c \
ft_strnfchridx.c \
ft_strnstr.c \
ft_strrchr.c \
ft_strtrim.c \

View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strfchridx.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/14 15:10:03 by khais #+# #+# */
/* Updated: 2025/02/14 15:16:39 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
#include <stdbool.h>
/*
** return the index of the first character c in the string s where f(c) returns
** true, or 0 if s is null, or length of s if c is not found.
*/
size_t ft_strfchridx(const char *s, bool f(char))
{
size_t i;
if (s == NULL)
return (0);
i = 0;
while (f(s[i]) == false && s[i] != '\0')
i++;
return (i);
}

View file

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnfchridx.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/14 15:16:49 by khais #+# #+# */
/* Updated: 2025/02/14 15:17:34 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
#include <stdbool.h>
/*
** return the index of the first character c in the string s where f(c) returns
** false, or 0 if s is null or c is not found.
*/
size_t ft_strnfchridx(const char *s, bool f(char))
{
size_t i;
if (s == NULL)
return (0);
i = 0;
while (f(s[i]) == true && s[i] != '\0')
i++;
if (s[i] == '\0')
return (0);
return (i);
}