#include <stdlib.h>
int atoi (const char *str);
atoi() converts a string str to an int
The function removes from the beginning of the string as many times as there are the following:
'\t' horizontal tab
'\n' newline
'\r' cariagge return
'\v' vertical tab
'\f' formfeed page break
' ' space
#include "libft.h"
int ft_atoi(const char *nptr)
{
int i;
int sign;
int num;
i = 0;
sign = 1;
num = 0;
while (nptr[i] == ' ' || nptr[i] == '\t' || nptr[i] == '\n' ||
nptr[i] == '\v' || nptr[i] == '\f' || nptr[i] == '\r')
i++;
if (nptr[i] == '-' || nptr[i] == '+')
{
if (nptr[i] == '-')
sign = -1;
i++;
}
while (nptr[i] >= '0' && nptr[i] <= '9')
{
num = num * 10 + (nptr[i] - '0');
i++;
}
return (num * sign);
}
#include <ctype.h>
int isalpha(int c);
The function checks if the character passed is an alphabet character.
returns 1 if true
returns 0 if false
#include "libft.h"
int ft_isalpha(int c)
{
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
return (1);
return (0);
}
#include <ctype.h>
int isdigit(int c);
The function checks if the character passed is a digit between 0 and 9
#include "libft.h"
int ft_isdigit(int c)
{
if (c >= '0' && c <= '9')
return (1);
return (0);
}
#include <ctype.h>
int isalnum(int c);
function checks if the character passed is a letter or number.
returns 1 if true
returns 0 if false
#include "libft.h"
int ft_isalnum(int c)
{
if ((c >= '0' && c <= '9') ||
(c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z'))
return (1);
else
return (0);
}
#include <ctype.h>
int isascii(int c);
The function tests if the character passed is in the ascii table.
#include "libft.h"
int ft_isascii(int c)
{
int y;
if (c >= 0 && c <= 127)
y = 1;
else
y = 0;
return (y);
}
#include <ctype.h>
int isprint(int c);
The function tests whether the character passed is printable.
#include "libft.h"
int ft_isprint(int c)
{
if (c >= 32 && c <= 126)
return (1);
return (0);
}
#include <ctype.h>
int toupper(int c);
The function converts an lowercase character to uppercase.
#include "libft.h"
int ft_toupper(int c)
{
if (c >= 'a' && c <= 'z')
c -= 32;
return (c);
}
#include <ctype.h>
int toupper(int c);
The function converts an uppercase character to lowercase.
#include "libft.h"
int ft_tolower(int c)
{
if (c >= 'A' && c <= 'Z')
c += 32;
return (c);
}
Description:
The memset() function fills the first n bytes of the memory area pointed to by s with the constant
byte c.
Return Value:
The memset() function returns a pointer to the memory area s.
#include "libft.h"
void* ft_memset(void *s, int c, size_t n)
{
size_t i;
unsigned char *voidptr;
i = 0;
voidptr = (unsigned char *)s;
while (i < n)
{
voidptr[i] = c;
i++;
}
return (s);
}
Description: The bzero() function erases the data in the n bytes of the memory starting at the location pointed to by s, by writing zeros (bytes containing '\0') to that area. Return value: none;
void ft_bzero(void *s, size_t n) { size_t i; unsigned char *voidptr; i = 0; voidptr = (unsigned char *)s; while (i < n) { voidptr[i] = 0; i++; } }
#include "libft.h"
void *ft_memcpy(void *dst, const void *src, size_t n)
{
char *d;
const char *s;
d = dst;
s = src;
if (dst == src)
return (0);
while (n--)
*d++ = *s++;
return (dst);
}
The memmove() function copies n bytes from memory area src to memory area dest. The memory areas may overlap: copying takes place as though the bytes in src are first copied into a temporary array that does not overlap src or dest, and the bytes are then copied from the temporary array to dest. The memmove() function returns a pointer to dest.
#include "libft.h"
void *ft_memmove(void *dst, const void *src, size_t n)
{
size_t i;
unsigned char *voidptr_dst;
unsigned char *voidptr_src;
i = 0;
voidptr_dst = (unsigned char *)dst;
voidptr_src = (unsigned char *)src;
if (src == NULL && dst == NULL)
return (NULL);
if (voidptr_dst < voidptr_src)
{
while (i < n)
{
voidptr_dst[i] = voidptr_src[i];
i++;
}
}
else
{
while (i < n)
{
voidptr_dst[n - 1 - i] = voidptr_src[n - 1 - i];
i++;
}
}
return (voidptr_dst);
}
Function searches for the first occurrence of the character c (an unsigned char) in the first n bytes of the string pointed to, by the argument s.
#include "libft.h"
void *ft_memchr(const void *s, int c, size_t n)
{
unsigned char *str;
size_t i;
str = (unsigned char *)s;
i = 0;
while (i < n)
{
if (str[i] == (unsigned char) c)
return (str + i);
i++;
}
return (NULL);
}
#include "libft.h"
int ft_memcmp(const void *s1, const void *s2, size_t n)
{
const unsigned char *str1;
const unsigned char *str2;
size_t i;
str1 = (const unsigned char *)s1;
str2 = (const unsigned char *)s2;
i = 0;
while (i < n)
{
if (str1[i] != str2[i])
return (str1[i] - str2[i]);
i++;
}
return (0);
}
#include "libft.h"
size_t ft_strlen(const char *s)
{
int i;
i = 0;
while (s[i])
i++;
return (i);
}
#include "libft.h"
size_t ft_strlcpy(char *dst, const char *src, size_t dstsize)
{
size_t i;
i = 0;
if (src == 0)
return (0);
if (dstsize > 0)
{
while (src[i] != '\0' && i < (dstsize - 1))
{
dst[i] = src[i];
i++;
}
dst[i] = '\0';
}
return (ft_strlen(src));
}
#include "libft.h"
size_t ft_strlcat(char *dst, const char *src, size_t dstsize)
{
size_t i;
size_t len_src;
size_t len_dst;
size_t j;
len_dst = ft_strlen(dst);
len_src = ft_strlen(src);
i = len_dst;
if (dst == src)
return (0);
if (dstsize != 0 && i < (dstsize - 1))
{
j = 0;
while (i < (dstsize - 1) && src[j] != '\0')
{
dst[i] = src[j];
i++;
j++;
}
dst[i] = '\0';
}
if (len_dst > dstsize)
return (dstsize + len_src);
return (len_dst + len_src);
}
#include <string.h>
char *strchr(const char *s, int c);
The function finds the first occurrence of c (converted to a char)
in the string pointed to by s.
\0 is considered part of the string
It returns the position of the character in the string
or NULL if the character is not found.
Here "character" means "byte"; these functions do not work with wide or multibyte characters.
#include "libft.h"
char *ft_strchr(const char *s, int c)
{
unsigned char uc;
uc = (unsigned char)c;
while (*s && *s != uc)
s++;
if (*s == uc)
return ((char*)s);
else
return (0);
}
#include <string.h>
char *strrchr(const char *s, int c);
The function finds the last occurrence of c (converted to a char)
in the string pointed to by s.
\0 is considered part of the string
It returns the position of the character in the string
or NULL if the character is not found.
#include "libft.h"
char *ft_strrchr(const char *s, int c)
{
int i;
i = 0;
while (s[i])
i++;
while (i >= 0)
{
if (s[i] == (char)c)
return ((char*)(s + i));
i--;
}
return (NULL);
}
#include <string.h>
char *strnstr(const char *big, const char *little, size_t len);
The function looks for the first occurrence of the string needle within the string haystack.
This search is limited by the len argument.
strnstr may have portability issues.
If needle is an empty string the function returns the haystack.
If needle is not in haystack, returns NULL.
If needle is found in haystack, the function returns the pointer of the first occurrence.
#include "libft.h"
char *ft_strnstr(const char *big, const char *little, size_t len)
{
size_t i;
size_t j;
i = 0;
j = 0;
if (*little == '\0')
return ((char *)big);
if (big == NULL && len == 0)
return (NULL);
while (big[i] != '\0' && i < len)
{
while (big[i + j] == little[j] && i + j < len)
{
j++;
if (little[j] == '\0')
return ((char *)big + i);
}
i++;
}
return (0);
}
#include "libft.h"
int ft_strncmp(const char *s1, const char *s2, size_t n)
{
size_t i;
i = 0;
while ((s1[i] != '\0' || s2[i] != '\0') && (i < n))
{
if (s1[i] != s2[i])
return (s1[i] - s2[i]);
i++;
}
return (0);
}
#include <stdlib.h>
void *calloc(size_t count, size_t size);
The function allocates the required memory and returns the pointer to the allocated space,
or NULL if the request fails
the difference between malloc() and calloc() is that malloc does not set the memory to zero where as calloc sets allocated memory to zero.
#include "libft.h"
void *ft_calloc(size_t nitems, size_t size)
{
char *str;
size_t i;
i = 0;
str = (char *)malloc(nitems * size);
if (str == 0)
return (0);
while (i < nitems * size)
{
str[i] = 0;
i++;
}
return ((void *)str);
}
This function returns a pointer to a null-terminated byte string, which is a duplicate of the string pointed to by s.
The memory obtained is done dynamically using malloc and hence it can be freed using free().
It returns a pointer to the duplicated string s.
#include "libft.h"
char *ft_strdup(const char *s1)
{
char *s1cpy;
size_t len;
size_t i;
len = ft_strlen(s1);
s1cpy = malloc((len + 1) * sizeof(char));
i = 0;
if (s1cpy == 0)
return (0);
while (i < len)
{
s1cpy[i] = s1[i];
i++;
}
s1cpy[i] = '\0';
return (s1cpy);
}
char *ft_substr(char const *s, unsigned int start, size_t len);
Allocates memory (using malloc) and returns a substring of the string 's'
The substring begins at index 'start' and is of maximum size 'len'.
Returns NULL if allocation fails.
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
size_t i;
size_t j;
char *subs;
if (!s)
return (NULL);
i = 0;
while (i < len && start < ft_strlen(s) && s[start + i])
i++;
j = 0;
subs = malloc((i + 1) * sizeof(char));
if (!subs)
return (NULL);
while (j < i && start < ft_strlen(s))
{
subs[j] = s[start + j];
j++;
}
subs[i] = '\0';
return (subs);
}
char *ft_strjoin(char const *s1, char const *s2);
Allocates memory (using malloc) and returns the string new product of the concatenation of s1 and s2,
or NULL if allocation fails.
#include "libft.h"
char *ft_strjoin(char const *s1, char const *s2)
{
char *nstr;
int i;
int j;
i = 0;
j = 0;
if (!s1 || !s2)
return (NULL);
nstr = (char *)malloc(sizeof(char) * ft_strlen(s1) + ft_strlen(s2) + 1);
if (!nstr)
return (NULL);
while (s1[i])
{
nstr[i] = s1[i];
i++;
}
while (s2[j])
{
nstr[i] = s2[j];
j++;
i++;
}
nstr[i] = '\0';
return (nstr);
}
Copyright © 2022 developed by ᐻᗠ๏ⲅ