ft_strlen
STRINGint ft_strlen(char *str)
{
int i;
i = 0;
while (str[i])
i++;
return (i);
}
Une documentation rapide pour retrouver une fonction, comprendre sa logique et revoir les bases sans chercher dans plusieurs fichiers.
Une fonction = une fiche indépendante.
int ft_strlen(char *str)
{
int i;
i = 0;
while (str[i])
i++;
return (i);
}
char *ft_strcpy(char *dest, char *src)
{
int i;
i = 0;
while (src[i])
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
return (dest);
}
int ft_strcmp(char *s1, char *s2)
{
int i;
i = 0;
while (s1[i] && s1[i] == s2[i])
i++;
return (s1[i] - s2[i]);
}
char *ft_strdup(char *src)
{
char *dup;
int i;
dup = malloc(sizeof(char) * (ft_strlen(src) + 1));
if (!dup)
return (NULL);
i = 0;
while (src[i])
{
dup[i] = src[i];
i++;
}
dup[i] = '\0';
return (dup);
}
char *ft_strchr(char *str, char c)
{
int i;
i = 0;
while (str[i])
{
if (str[i] == c)
return (&str[i]);
i++;
}
if (c == '\0')
return (&str[i]);
return (NULL);
}
char *ft_strjoin(char *s1, char *s2)
{
char *str;
int i;
int j;
str = malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1));
if (!str)
return (NULL);
i = 0;
while (s1[i])
{
str[i] = s1[i];
i++;
}
j = 0;
while (s2[j])
{
str[i + j] = s2[j];
j++;
}
str[i + j] = '\0';
return (str);
}
Allocation et manipulation de mémoire.
char *str;
str = malloc(sizeof(char) * 10);
if (!str)
return ;
/* utilisation */
free(str);
void *ft_memset(void *ptr, int value, size_t size)
{
size_t i;
unsigned char *p;
p = ptr;
i = 0;
while (i < size)
{
p[i] = (unsigned char)value;
i++;
}
return (ptr);
}
void *ft_memcpy(void *dst, void *src, size_t n)
{
size_t i;
unsigned char *d;
unsigned char *s;
d = dst;
s = src;
i = 0;
while (i < n)
{
d[i] = s[i];
i++;
}
return (dst);
}
Tests et transformations de caractères.
int ft_isalpha(char c)
{
return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'));
}
int ft_isdigit(char c)
{
return (c >= '0' && c <= '9');
}
int ft_isalnum(char c)
{
return (ft_isalpha(c) || ft_isdigit(c));
}
int ft_isascii(char c)
{
return (c >= 0 && c <= 127);
}
int ft_isprint(char c)
{
return (c >= 32 && c <= 126);
}
char ft_toupper(char c)
{
if (c >= 'a' && c <= 'z')
return (c - 32);
return (c);
}
char ft_tolower(char c)
{
if (c >= 'A' && c <= 'Z')
return (c + 32);
return (c);
}
Les petits algorithmes classiques d'évaluation.
void ft_swap(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
void ft_rev_int_tab(int *tab, int size)
{
int i;
int tmp;
i = 0;
while (i < size / 2)
{
tmp = tab[i];
tab[i] = tab[size - 1 - i];
tab[size - 1 - i] = tmp;
i++;
}
}
void ft_sort_int_tab(int *tab, int size)
{
int i;
int j;
int tmp;
i = 0;
while (i < size - 1)
{
j = i + 1;
while (j < size)
{
if (tab[i] > tab[j])
{
tmp = tab[i];
tab[i] = tab[j];
tab[j] = tmp;
}
j++;
}
i++;
}
}
int ft_iterative_power(int nb, int power)
{
int result;
if (power < 0)
return (0);
result = 1;
while (power--)
result *= nb;
return (result);
}
int ft_recursive_power(int nb, int power)
{
if (power < 0)
return (0);
if (power == 0)
return (1);
return (nb * ft_recursive_power(nb, power - 1));
}
int *ft_range(int min, int max)
{
int *tab;
int i;
if (min >= max)
return (NULL);
tab = malloc(sizeof(int) * (max - min));
if (!tab)
return (NULL);
i = 0;
while (min < max)
{
tab[i] = min;
i++;
min++;
}
return (tab);
}
Les bases de read, write et des fichiers.
void ft_putchar(char c)
{
write(1, &c, 1);
}
void ft_putstr(char *str)
{
int i;
i = 0;
while (str[i])
{
write(1, &str[i], 1);
i++;
}
}
void ft_putnbr(int nb)
{
char c;
if (nb == -2147483648)
{
write(1, "-2147483648", 11);
return ;
}
if (nb < 0)
{
write(1, "-", 1);
nb = -nb;
}
if (nb >= 10)
ft_putnbr(nb / 10);
c = nb % 10 + '0';
write(1, &c, 1);
}
char buf[1024];
int ret;
ret = read(fd, buf, 1024);
if (ret > 0)
write(1, buf, ret);
fd = open(file, O_RDONLY);
if (fd < 0)
return ;
/* utilisation */
close(fd);
Découper et interpréter les chaînes.
int ft_atoi(char *str)
{
int i;
int sign;
int nbr;
i = 0;
sign = 1;
nbr = 0;
while (str[i] == ' ' || (str[i] >= 9 && str[i] <= 13))
i++;
if (str[i] == '-' || str[i] == '+')
{
if (str[i] == '-')
sign = -1;
i++;
}
while (str[i] >= '0' && str[i] <= '9')
{
nbr = nbr * 10 + str[i] - '0';
i++;
}
return (nbr * sign);
}
#include <stdlib.h>
static int count_words(char *str, char sep)
{
int i;
int count;
i = 0;
count = 0;
while (str[i])
{
while (str[i] == sep)
i++;
if (str[i])
count++;
while (str[i] && str[i] != sep)
i++;
}
return (count);
}
static int word_len(char *str, char sep)
{
int len;
len = 0;
while (str[len] && str[len] != sep)
len++;
return (len);
}
static char *copy_word(char *str, int len)
{
char *word;
int i;
word = malloc(sizeof(char) * (len + 1));
if (!word)
return (NULL);
i = 0;
while (i < len)
{
word[i] = str[i];
i++;
}
word[i] = '\0';
return (word);
}
char **ft_split(char *str, char sep)
{
char **tab;
int i;
int j;
int len;
tab = malloc(sizeof(char *) * (count_words(str, sep) + 1));
if (!tab)
return (NULL);
i = 0;
j = 0;
while (str[i])
{
while (str[i] == sep)
i++;
if (!str[i])
break ;
len = word_len(&str[i], sep);
tab[j] = copy_word(&str[i], len);
if (!tab[j])
return (NULL);
j++;
i += len;
}
tab[j] = NULL;
return (tab);
}
int find_char(char *str, char c)
{
int i;
i = 0;
while (str[i])
{
if (str[i] == c)
return (i);
i++;
}
return (-1);
}
int count_char(char *str, char c)
{
int i;
int count;
i = 0;
count = 0;
while (str[i])
{
if (str[i] == c)
count++;
i++;
}
return (count);
}
Les opérations classiques sur les listes.
t_list *ft_lstnew(void *content)
{
t_list *node;
node = malloc(sizeof(t_list));
if (!node)
return (NULL);
node->content = content;
node->next = NULL;
return (node);
}
void ft_lstadd_front(t_list **lst, t_list *new)
{
if (!lst || !new)
return ;
new->next = *lst;
*lst = new;
}
Reconnaître rapidement le type d'exercice.
while (str[i])ft_strlen'\0'ft_atoift_splitmalloc + vérification + freeopen → read → closewrite