Up until ex01

This commit is contained in:
tosu 2023-03-31 17:42:41 +02:00
commit ac52de73f3
3 changed files with 106 additions and 0 deletions

43
ex00/ft_foreach.c Normal file
View File

@ -0,0 +1,43 @@
void ft_foreach(int *tab, int length, void(*f)(int))
{
while (length--)
f(*tab++);
}
/* ////
#include <limits.h>
#include <unistd.h>
void ft_putchar(char c)
{
write(1, &c, 1);
}
void ft_putnbr(int nb)
{
if (nb > 9)
{
ft_putnbr(nb / 10);
ft_putchar(nb % 10 + '0');
}
else if (nb == INT_MIN)
{
ft_putnbr(nb / 10);
ft_putnbr(-(nb % 10));
}
else if (nb < 0)
{
ft_putchar('-');
ft_putnbr(-nb);
}
else
ft_putchar(nb % 10 + '0');
}
int main(void)
{
int ints[10] = {1, 2, 0, INT_MIN, 10, -1, 5, 0, 7, INT_MAX};
ft_foreach(ints, 10, &ft_putnbr);
return (0);
}
*/ ////

35
ex01/ft_map.c Normal file
View File

@ -0,0 +1,35 @@
#include <stdlib.h>
int *ft_map(int *tab, int length, int(*f)(int))
{
int *t;
int i;
t = malloc(sizeof(int) * length);
i = -1;
while (++i < length)
t[i] = f(tab[i]);
return (t);
}
/* ////
#include <stdio.h>
#include <limits.h>
int inc(int n)
{
return (n + 1);
}
int main(void)
{
int ints[10] = {1, 2, 0, INT_MIN, 10, -1, 5, 0, 7, INT_MAX};
int *ints2;
ints2 = ft_map(ints, 10, &inc);
for (int i = 0; i < 10; ++i)
printf("%d\n", ints2[i]);
return (0);
}
*/ ////

28
ex02/ft_any.c Normal file
View File

@ -0,0 +1,28 @@
#include <stdlib.h>
int ft_any(char **tab, int(*f)(char*))
{
while (*tab)
if (f(*tab++))
return (1);
return (0);
}
#include <stdio.h>
int ft_is_empty(char *str)
{
return (*str == 0);
}
int main(void)
{
char **strs;
strs = malloc(sizeof(char *) * 4);
strs[0] = "Hey";
strs[1] = "s";
strs[2] = "Ho";
strs[3] = "";
printf("%d\n", ft_any(strs, &ft_is_empty));
free(strs);
}