├── author ├── README.md ├── ft_cntwrd.c ├── ft_memdel.c ├── ft_putchar.c ├── Makefile ├── ft_strdel.c ├── ft_putchar_fd.c ├── ft_putstr_fd.c ├── ft_putstr.c ├── ft_lstiter.c ├── ft_strcpy.c ├── ft_putendl.c ├── ft_strcat.c ├── ft_strncpy.c ├── ft_isascii.c ├── ft_isdigit.c ├── ft_strclr.c ├── ft_isprint.c ├── ft_strdup.c ├── ft_strncat.c ├── ft_memchr.c ├── ft_putendl_fd.c ├── ft_isalnum.c ├── ft_tolower.c ├── ft_isalpha.c ├── ft_strlen.c ├── ft_toupper.c ├── ft_memset.c ├── ft_memalloc.c ├── ft_bzero.c ├── ft_strnew.c ├── ft_putnbr_fd.c ├── ft_strcmp.c ├── ft_strequ.c ├── ft_strlcat.c ├── ft_lstmap.c ├── ft_lstdelone.c ├── ft_strrchr.c ├── ft_atoi.c ├── ft_lstdel.c ├── ft_memcpy.c ├── ft_memcmp.c ├── ft_strncmp.c ├── ft_striter.c ├── ft_strnequ.c ├── ft_strstr.c ├── ft_strchr.c ├── ft_lstadd.c ├── ft_striteri.c ├── ft_memmove.c ├── ft_strsub.c ├── ft_memccpy.c ├── ft_strmap.c ├── ft_strjoin.c ├── ft_strnstr.c ├── ft_strmapi.c ├── ft_itoa.c ├── ft_strtrim.c ├── ft_lstnew.c ├── ft_strsplit.c ├── ft_putnbr.c └── libft.h /author: -------------------------------------------------------------------------------- 1 | obibik 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 42_libft 2 | The aim of this project is to code a C library regrouping usual functions that you’ll be allowed to use in all your other projects. 3 | 4 | 5 | A list of C programming exercises: 6 | 1) https://www.sanfoundry.com/simple-c-programs/ 7 | 2) https://www.w3resource.com/c-programming-exercises/file-handling/index.php 8 | 3) http://www.c4learn.com/learn-c-programming-language/ 9 | 4) https://www.tutorialspoint.com/learn_c_by_examples/index.htm 10 | 5) https://www.learn-html.org/ 11 | 6) https://beginnersbook.com/2015/02/simple-c-programs/ 12 | 7) https://www.programmingsimplified.com/c-program-examples 13 | -------------------------------------------------------------------------------- /ft_cntwrd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_cntwrd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/28 12:41:01 by obibik #+# #+# */ 9 | /* Updated: 2018/08/28 12:41:28 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** Extra function for ft_strsplit.c 15 | */ 16 | 17 | #include "libft.h" 18 | 19 | int ft_cntwrd(char const *s, char c) 20 | { 21 | unsigned int i; 22 | int cntr; 23 | 24 | i = 0; 25 | cntr = 0; 26 | while (s[i]) 27 | { 28 | while (s[i] == c) 29 | i++; 30 | if (s[i] != '\0') 31 | cntr++; 32 | while (s[i] && (s[i] != c)) 33 | i++; 34 | } 35 | return (cntr); 36 | } 37 | -------------------------------------------------------------------------------- /ft_memdel.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memdel.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/22 16:55:14 by obibik #+# #+# */ 9 | /* Updated: 2018/08/22 16:55:25 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** Takes as a parameter the address of a memory area that needs 15 | ** to be freed with free(3), then puts the pointer to NULL. 16 | ** Param. #1 A pointer’s address that needs its memory freed and set to 17 | ** NULL. 18 | ** Return value None. 19 | ** Libc functions free(3). 20 | */ 21 | 22 | #include "libft.h" 23 | 24 | void ft_memdel(void **ap) 25 | { 26 | free(*ap); 27 | *ap = NULL; 28 | } 29 | -------------------------------------------------------------------------------- /ft_putchar.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putchar.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/22 16:55:36 by obibik #+# #+# */ 9 | /* Updated: 2018/08/22 16:55:38 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** Outputs the character c to the standard output. 15 | ** Param. #1 The character to output. 16 | ** Return value None. 17 | ** Libc functions write(2). 18 | */ 19 | 20 | #include "libft.h" 21 | 22 | void ft_putchar(char c) 23 | { 24 | write(1, &c, 1); 25 | } 26 | 27 | /* 28 | ** int main () { 29 | ** char letter = 'A'; 30 | ** ft_putchar(letter); 31 | ** return(0); 32 | ** } 33 | */ 34 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # **************************************************************************** # 2 | # # 3 | # ::: :::::::: # 4 | # Makefile :+: :+: :+: # 5 | # +:+ +:+ +:+ # 6 | # By: obibik +#+ +:+ +#+ # 7 | # +#+#+#+#+#+ +#+ # 8 | # Created: 2018/08/22 17:36:24 by obibik #+# #+# # 9 | # Updated: 2018/08/22 17:36:28 by obibik ### ########.fr # 10 | # # 11 | # **************************************************************************** # 12 | 13 | NAME = libft.a 14 | CC = gcc -c 15 | INCLUDE = libft.h 16 | FLAGS = -Wall -Wextra -Werror 17 | AR = ar rc 18 | 19 | SRCS = *.c 20 | 21 | OBJ = *.o 22 | 23 | all: $(NAME) 24 | 25 | $(NAME): 26 | $(CC) $(FLAGS) $(SRCS) 27 | $(AR) $(NAME) $(OBJ) 28 | ranlib $(NAME) 29 | 30 | clean: 31 | /bin/rm -f $(OBJ) 32 | 33 | fclean: clean 34 | /bin/rm -rf $(NAME) 35 | 36 | re: fclean all 37 | 38 | .PHONY: all clean fclean re 39 | -------------------------------------------------------------------------------- /ft_strdel.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strdel.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/22 16:57:55 by obibik #+# #+# */ 9 | /* Updated: 2018/08/22 16:57:58 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** Takes as a parameter the address of a string that need to be 15 | ** freed with free(3), then sets its pointer to NULL. 16 | ** Param. #1 The string’s address that needs to be freed and its pointer set 17 | ** to NULL. 18 | ** Return value None. 19 | ** Libc functions Free(3) 20 | */ 21 | 22 | #include "libft.h" 23 | 24 | void ft_strdel(char **as) 25 | { 26 | if (as) 27 | { 28 | free(*as); 29 | *as = NULL; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /ft_putchar_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putchar_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/22 16:55:47 by obibik #+# #+# */ 9 | /* Updated: 2018/08/22 16:55:49 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** Outputs the char c to the file descriptor fd. 15 | ** Param. #1 The character to output. 16 | ** Param. #2 The file descriptor. 17 | ** Return value None. 18 | ** Libc functions write(2). 19 | */ 20 | 21 | #include "libft.h" 22 | 23 | void ft_putchar_fd(char c, int fd) 24 | { 25 | write(fd, &c, 1); 26 | } 27 | 28 | /* 29 | ** int main () 30 | ** { 31 | ** char letter = 'A'; 32 | ** ft_putchar_fd(letter, 2); 33 | ** return(0); 34 | ** } 35 | */ 36 | -------------------------------------------------------------------------------- /ft_putstr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putstr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/22 16:57:23 by obibik #+# #+# */ 9 | /* Updated: 2018/08/22 16:57:27 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** Outputs the string s to the file descriptor fd. 15 | ** Param. #1 The string to output. 16 | ** Param. #2 The file descriptor. 17 | ** Return value None. 18 | ** Libc functions write(2). 19 | */ 20 | 21 | #include "libft.h" 22 | 23 | void ft_putstr_fd(char const *s, int fd) 24 | { 25 | write(fd, s, ft_strlen(s)); 26 | } 27 | 28 | /* 29 | ** int main () { 30 | ** char letter[20] = "Life if good."; 31 | ** ft_putstr_fd(letter, 1); 32 | ** return(0); 33 | ** } 34 | */ 35 | -------------------------------------------------------------------------------- /ft_putstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/22 16:57:09 by obibik #+# #+# */ 9 | /* Updated: 2018/08/22 16:57:11 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** Outputs the string s to the standard output. 15 | ** Param. #1 The string to output. 16 | ** Return value None. 17 | ** Libc functions write(2). 18 | */ 19 | 20 | #include "libft.h" 21 | 22 | void ft_putstr(char const *s) 23 | { 24 | int i; 25 | 26 | i = 0; 27 | while (s[i]) 28 | { 29 | ft_putchar(s[i]); 30 | i++; 31 | } 32 | } 33 | 34 | /* 35 | ** int main () { 36 | ** char letter[20] = "Life if good."; 37 | ** ft_putstr(letter); 38 | ** return(0); 39 | ** } 40 | */ 41 | -------------------------------------------------------------------------------- /ft_lstiter.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstiter.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/22 17:46:46 by obibik #+# #+# */ 9 | /* Updated: 2018/08/22 17:46:49 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** Description Iterates the list lst and applies the function f to each link. 15 | ** Param. #1 A pointer to the first link of a list. 16 | ** Param. #2 The address of a function to apply to each link of a list. 17 | ** Return value None. 18 | ** Libc functions None 19 | */ 20 | 21 | #include "libft.h" 22 | 23 | void ft_lstiter(t_list *lst, void (*f)(t_list *elem)) 24 | { 25 | if (f != NULL) 26 | { 27 | while (lst != NULL) 28 | { 29 | f(lst); 30 | lst = lst->next; 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /ft_strcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/14 17:30:26 by obibik #+# #+# */ 9 | /* Updated: 2018/08/14 17:30:29 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** stpcpy, stpncpy, strcpy, strncpy -- copy strings 15 | */ 16 | 17 | #include "libft.h" 18 | 19 | char *ft_strcpy(char *dst, const char *src) 20 | { 21 | int i; 22 | 23 | i = 0; 24 | while (src[i] != '\0') 25 | { 26 | dst[i] = src[i]; 27 | i++; 28 | } 29 | dst[i] = src[i]; 30 | return (dst); 31 | } 32 | 33 | /* 34 | ** int main () 35 | ** { 36 | ** char example[50]; 37 | ** 38 | ** ft_strcpy(example, "Life is good!"); 39 | ** printf("%s\n", example); 40 | ** 41 | ** return(0); 42 | ** } 43 | */ 44 | -------------------------------------------------------------------------------- /ft_putendl.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putendl.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/22 16:56:00 by obibik #+# #+# */ 9 | /* Updated: 2018/08/22 16:56:02 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** Outputs the string s to the standard output followed by a ’\n’. 15 | ** Param. #1 The string to output. 16 | ** Return value None. 17 | ** Libc functions write(2). 18 | ** 19 | ** 20 | ** This function will display the string s on the standard output followed by a 21 | **'\n', which is a new line. 22 | */ 23 | 24 | #include "libft.h" 25 | 26 | void ft_putendl(char const *s) 27 | { 28 | ft_putstr(s); 29 | ft_putchar('\n'); 30 | } 31 | 32 | /* 33 | ** int main() 34 | ** { 35 | ** char str[20] = "Life is good."; 36 | ** ft_putendl(str); 37 | ** return (0); 38 | ** } 39 | */ 40 | -------------------------------------------------------------------------------- /ft_strcat.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strcat.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/14 17:31:57 by obibik #+# #+# */ 9 | /* Updated: 2018/08/14 17:32:00 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** strcat, strncat -- concatenate strings 15 | */ 16 | 17 | #include "libft.h" 18 | 19 | char *ft_strcat(char *s1, const char *s2) 20 | { 21 | int i; 22 | int j; 23 | 24 | i = 0; 25 | j = 0; 26 | while (s1[i] != '\0') 27 | i++; 28 | while (s2[j] != '\0') 29 | { 30 | s1[i + j] = s2[j]; 31 | j++; 32 | } 33 | s1[i + j] = '\0'; 34 | return (s1); 35 | } 36 | 37 | /* 38 | ** int main () { 39 | ** char s1[50] = "This is s1. "; 40 | ** char s2[50] = "This is s2."; 41 | ** 42 | ** ft_strcat(s1, s2); 43 | ** printf("Final string : %s\n", s1); 44 | ** 45 | ** return(0); 46 | ** } 47 | */ 48 | -------------------------------------------------------------------------------- /ft_strncpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strncpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/14 17:32:09 by obibik #+# #+# */ 9 | /* Updated: 2018/08/14 17:32:11 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** stpcpy, stpncpy, strcpy, strncpy -- copy strings 15 | */ 16 | 17 | #include "libft.h" 18 | 19 | char *ft_strncpy(char *dest, const char *src, size_t len) 20 | { 21 | size_t i; 22 | 23 | i = 0; 24 | while (src[i] != '\0' && (i < len)) 25 | { 26 | dest[i] = src[i]; 27 | i++; 28 | } 29 | while (i < len) 30 | { 31 | dest[i] = '\0'; 32 | i++; 33 | } 34 | return (dest); 35 | } 36 | 37 | /* 38 | ** int main () 39 | ** { 40 | ** char example[50]; 41 | ** 42 | ** ft_strncpy(example, "Life is good!", 9); 43 | ** printf("%s\n", example); 44 | ** 45 | ** return(0); 46 | ** } 47 | */ 48 | -------------------------------------------------------------------------------- /ft_isascii.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isascii.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/14 17:35:41 by obibik #+# #+# */ 9 | /* Updated: 2018/08/14 17:35:44 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** isascii -- test for ASCII character 15 | */ 16 | 17 | #include "libft.h" 18 | 19 | int ft_isascii(int c) 20 | { 21 | return (c >= 0 && c <= 127); 22 | } 23 | 24 | /* 25 | ** int main() 26 | ** { 27 | ** char c, result; 28 | ** 29 | ** c = '7'; 30 | ** result = ft_isascii(c); 31 | ** printf("The result is %d\n", result); 32 | ** 33 | ** c = 'g'; 34 | ** result = ft_isascii(c); 35 | ** printf("The result is %d\n", result); 36 | ** 37 | ** c = '+'; 38 | ** result = ft_isascii(c); 39 | ** printf("The result is %d\n", result); 40 | ** 41 | ** return 0; 42 | ** } 43 | */ 44 | -------------------------------------------------------------------------------- /ft_isdigit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isdigit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/14 17:35:16 by obibik #+# #+# */ 9 | /* Updated: 2018/08/14 17:35:18 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** isdigit, isnumber -- decimal-digit character test 15 | */ 16 | 17 | #include "libft.h" 18 | 19 | int ft_isdigit(int c) 20 | { 21 | return (c <= '9' && c >= '0'); 22 | } 23 | 24 | /* 25 | ** int main() 26 | ** { 27 | ** char c, result; 28 | ** 29 | ** c = '7'; 30 | ** result = ft_isdigit(c); 31 | ** printf("The result is %d\n", result); 32 | ** 33 | ** c = 'g'; 34 | ** result = ft_isdigit(c); 35 | ** printf("The result is %d\n", result); 36 | ** 37 | ** c = '+'; 38 | ** result = ft_isdigit(c); 39 | ** printf("The result is %d\n", result); 40 | ** 41 | ** return 0; 42 | ** } 43 | */ 44 | -------------------------------------------------------------------------------- /ft_strclr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strclr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/22 16:57:40 by obibik #+# #+# */ 9 | /* Updated: 2018/08/22 16:57:43 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** Description Sets every character of the string to the value ’\0’. 15 | ** Param. #1 The string that needs to be cleared. 16 | ** Return value None. 17 | ** Libc functions None. 18 | */ 19 | 20 | #include "libft.h" 21 | 22 | void ft_strclr(char *s) 23 | { 24 | unsigned int i; 25 | 26 | if (!s) 27 | return ; 28 | i = 0; 29 | while (s[i] != '\0') 30 | { 31 | s[i] = '\0'; 32 | i++; 33 | } 34 | } 35 | 36 | /* 37 | ** int main() 38 | ** { 39 | ** char str[10] = "Hello."; 40 | ** printf("The result is %s\n", str); 41 | ** ft_strclr(str); 42 | ** printf("The result is %s\n", str); 43 | ** return 0; 44 | ** } 45 | */ 46 | -------------------------------------------------------------------------------- /ft_isprint.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isprint.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/14 17:36:04 by obibik #+# #+# */ 9 | /* Updated: 2018/08/14 17:36:12 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** isprint -- printing character test (space character inclusive) 15 | */ 16 | 17 | #include "libft.h" 18 | 19 | int ft_isprint(int c) 20 | { 21 | return (c >= 32 && c < 127); 22 | } 23 | 24 | /* 25 | ** int main() 26 | ** { 27 | ** char c, result; 28 | ** 29 | ** c = 'NUL'; 30 | ** result = ft_isprint(c); 31 | ** printf("The result is %d\n", result); 32 | ** 33 | ** c = 'g'; 34 | ** result = ft_isprint(c); 35 | ** printf("The result is %d\n", result); 36 | ** 37 | ** c = '+'; 38 | ** result = ft_isprint(c); 39 | ** printf("The result is %d\n", result); 40 | ** 41 | ** return 0; 42 | ** } 43 | */ 44 | -------------------------------------------------------------------------------- /ft_strdup.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strdup.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/14 17:30:12 by obibik #+# #+# */ 9 | /* Updated: 2018/08/14 17:30:15 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** strdup, strndup -- save a copy of a string 15 | */ 16 | 17 | #include "libft.h" 18 | 19 | char *ft_strdup(const char *src) 20 | { 21 | char *dup; 22 | int i; 23 | 24 | i = 0; 25 | while (src[i] != '\0') 26 | i++; 27 | dup = (char *)malloc(i + 1); 28 | if (dup == NULL) 29 | return (0); 30 | i = 0; 31 | while (src[i] != '\0') 32 | { 33 | dup[i] = src[i]; 34 | i++; 35 | } 36 | dup[i] = '\0'; 37 | return (dup); 38 | } 39 | 40 | /* 41 | ** int main() 42 | ** { 43 | ** char str[] = "GeeksForGeeks"; 44 | ** char* result = ft_strdup(str); 45 | ** printf("%s", result); 46 | ** return 0; 47 | ** } 48 | */ 49 | -------------------------------------------------------------------------------- /ft_strncat.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strncat.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/14 17:32:28 by obibik #+# #+# */ 9 | /* Updated: 2018/08/14 17:32:33 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** strcat, strncat -- concatenate strings 15 | */ 16 | 17 | #include "libft.h" 18 | 19 | char *ft_strncat(char *s1, const char *s2, size_t n) 20 | { 21 | size_t i; 22 | size_t j; 23 | 24 | i = 0; 25 | j = 0; 26 | while (s1[i] != '\0') 27 | i++; 28 | while (s2[j] != '\0' && j < n) 29 | { 30 | s1[i + j] = s2[j]; 31 | j++; 32 | } 33 | s1[i + j] = '\0'; 34 | return (s1); 35 | } 36 | 37 | /* 38 | ** int main () { 39 | ** char s1[50] = "This is s1. "; 40 | ** char s2[50] = "This is s2."; 41 | ** 42 | ** ft_strncat(s1, s2, 5); 43 | ** printf("Final string : %s\n", s1); 44 | ** 45 | ** return(0); 46 | ** } 47 | */ 48 | -------------------------------------------------------------------------------- /ft_memchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/14 17:29:11 by obibik #+# #+# */ 9 | /* Updated: 2018/08/14 17:29:15 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** memchr -- locate byte in byte string 15 | */ 16 | 17 | #include "libft.h" 18 | 19 | void *ft_memchr(const void *s, int c, size_t n) 20 | { 21 | const char *sc; 22 | size_t i; 23 | 24 | sc = (const char *)s; 25 | i = -1; 26 | while (++i < n) 27 | if (*(sc + i) == (char)c) 28 | return ((void *)sc + i); 29 | return (NULL); 30 | } 31 | 32 | /* 33 | ** int main () { 34 | ** const char str[] = "http://www.tutorialspoint.com"; 35 | ** const char ch = '.'; 36 | ** char *result; 37 | ** 38 | ** result = memchr(str, ch, 11); 39 | ** 40 | ** printf("String after character is %s\n", result); 41 | ** 42 | ** return(0); 43 | ** } 44 | */ 45 | -------------------------------------------------------------------------------- /ft_putendl_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putendl_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/22 16:56:14 by obibik #+# #+# */ 9 | /* Updated: 2018/08/22 16:56:17 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** Outputs the string s to the file descriptor fd followed by a 15 | ** ’\n’. 16 | ** Param. #1 The string to output. 17 | ** Param. #2 The file descriptor. 18 | ** Return value None. 19 | ** Libc functions write(2). 20 | ** 21 | ** 22 | ** This function writes the string s to the file descriptor fd followed by a 23 | ** '\n' to make a new line. 24 | */ 25 | 26 | #include "libft.h" 27 | 28 | void ft_putendl_fd(char const *s, int fd) 29 | { 30 | ft_putstr_fd(s, fd); 31 | ft_putchar_fd('\n', fd); 32 | } 33 | 34 | /* 35 | ** int main() 36 | ** { 37 | ** char str[20] = "Life is good."; 38 | ** ft_putendl_fd(str, 1); 39 | ** return (0); 40 | ** } 41 | */ 42 | -------------------------------------------------------------------------------- /ft_isalnum.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalnum.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/14 17:35:29 by obibik #+# #+# */ 9 | /* Updated: 2018/08/14 17:35:31 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** isalnum -- alphanumeric character test 15 | */ 16 | 17 | #include "libft.h" 18 | 19 | int ft_isalnum(int c) 20 | { 21 | if (ft_isalpha(c) || ft_isdigit(c)) 22 | return (1); 23 | else 24 | return (0); 25 | } 26 | 27 | /* 28 | ** int main() 29 | ** { 30 | ** char c, result; 31 | ** 32 | ** c = '*'; 33 | ** result = ft_isalnum(c); 34 | ** printf("The result is %d\n", result); 35 | ** 36 | ** c = 'g'; 37 | ** result = ft_isalnum(c); 38 | ** printf("The result is %d\n", result); 39 | ** 40 | ** c = '+'; 41 | ** result = ft_isalnum(c); 42 | ** printf("The result is %d\n", result); 43 | ** 44 | ** return 0; 45 | ** } 46 | */ 47 | -------------------------------------------------------------------------------- /ft_tolower.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_tolower.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/14 17:37:01 by obibik #+# #+# */ 9 | /* Updated: 2018/08/14 17:37:03 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** tolower, tolower_l -- upper case to lower case letter conversion 15 | */ 16 | 17 | #include "libft.h" 18 | 19 | int ft_tolower(int c) 20 | { 21 | if (c >= 65 && c <= 90) 22 | return (c + 32); 23 | return (c); 24 | } 25 | 26 | /* 27 | ** int main() 28 | ** { 29 | ** char c, result; 30 | ** 31 | ** c = 'M'; 32 | ** result = ft_tolower(c); 33 | ** printf("The result is %c\n", result); 34 | ** 35 | ** c = 'g'; 36 | ** result = ft_tolower(c); 37 | ** printf("The result is %c\n", result); 38 | ** 39 | ** c = '+'; 40 | ** result = ft_tolower(c); 41 | ** printf("The result is %c\n", result); 42 | ** 43 | ** return 0; 44 | ** } 45 | */ 46 | -------------------------------------------------------------------------------- /ft_isalpha.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_isalpha.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/14 17:35:01 by obibik #+# #+# */ 9 | /* Updated: 2018/08/14 17:35:03 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** isalpha -- alphabetic character test 15 | */ 16 | 17 | #include "libft.h" 18 | 19 | int ft_isalpha(int c) 20 | { 21 | if ((c >= 65 && c <= 90) || (c >= 97 & c <= 122)) 22 | return (1); 23 | else 24 | return (0); 25 | } 26 | 27 | /* 28 | ** int main() 29 | ** { 30 | ** char c, result; 31 | ** 32 | ** c = '*'; 33 | ** result = ft_isalpha(c); 34 | ** printf("The result is %d\n", result); 35 | ** 36 | ** c = 'g'; 37 | ** result = ft_isalpha(c); 38 | ** printf("The result is %d\n", result); 39 | ** 40 | ** c = '+'; 41 | ** result = ft_isalpha(c); 42 | ** printf("The result is %d\n", result); 43 | ** 44 | ** return 0; 45 | ** } 46 | */ 47 | -------------------------------------------------------------------------------- /ft_strlen.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlen.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/14 17:29:50 by obibik #+# #+# */ 9 | /* Updated: 2018/08/14 17:30:00 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** strlen, strnlen -- find length of string 15 | ** 16 | ** We use a size_t because it is guaranteed to be big enough to contain 17 | ** the size of the biggest object your system can handle. This way we can 18 | ** display the absolute biggest string that our computer can handle. 19 | */ 20 | 21 | #include "libft.h" 22 | 23 | size_t ft_strlen(const char *s) 24 | { 25 | size_t i; 26 | 27 | i = 0; 28 | while (s[i] != '\0') 29 | i++; 30 | return (i); 31 | } 32 | 33 | /* 34 | ** int main() 35 | ** { 36 | ** char str[] = "Life is good."; 37 | ** int result = ft_strlen(str); 38 | ** printf("The length of the string is %d.\n", result); 39 | ** return 0; 40 | ** } 41 | */ 42 | -------------------------------------------------------------------------------- /ft_toupper.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_toupper.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/14 17:36:47 by obibik #+# #+# */ 9 | /* Updated: 2018/08/14 17:36:50 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** toupper, toupper_l -- lower case to upper case letter conversion 15 | */ 16 | 17 | #include "libft.h" 18 | 19 | int ft_toupper(int c) 20 | { 21 | if (c >= 97 && c <= 122) 22 | return (c - 32); 23 | return (c); 24 | } 25 | 26 | /* 27 | ** int main() 28 | ** { 29 | ** char c, result; 30 | ** 31 | ** c = 'M'; 32 | ** result = ft_toupper(c); 33 | ** printf("The result is %c\n", result); 34 | ** 35 | ** c = 'g'; 36 | ** result = ft_toupper(c); 37 | ** printf("The result is %c\n", result); 38 | ** 39 | ** c = '+'; 40 | ** result = ft_toupper(c); 41 | ** printf("The result is %c\n", result); 42 | ** 43 | ** return 0; 44 | ** } 45 | */ 46 | -------------------------------------------------------------------------------- /ft_memset.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memset.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/14 12:12:09 by obibik #+# #+# */ 9 | /* Updated: 2018/08/14 13:08:57 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** memset -- fill a byte string with a byte value. 15 | ** The memset() function writes len bytes of value c 16 | ** (converted to an unsigned char) to the string b. 17 | ** The memset() function returns its first argument. 18 | */ 19 | 20 | #include "libft.h" 21 | 22 | void *ft_memset(void *b, int c, size_t len) 23 | { 24 | char *p; 25 | 26 | p = (char *)b; 27 | while (len > 0) 28 | { 29 | *p = c; 30 | p++; 31 | len--; 32 | } 33 | return (b); 34 | } 35 | 36 | /* 37 | ** int main() 38 | ** { 39 | ** char str[100] = "Libc is the standard library."; 40 | ** 41 | ** ft_memset(str, '.', 8 * sizeof(char)); 42 | ** printf("After memset(): %s\n", str); 43 | ** return 0; 44 | ** } 45 | */ 46 | -------------------------------------------------------------------------------- /ft_memalloc.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memalloc.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/22 16:55:03 by obibik #+# #+# */ 9 | /* Updated: 2018/08/22 16:55:05 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** Allocates (with malloc(3)) and returns a “fresh” memory 15 | ** area. The memory allocated is initialized to 0. If the allocation 16 | ** fails, the function returns NULL. 17 | ** Param. #1 The size of the memory that needs to be allocated. 18 | ** Return value The allocated memory area. 19 | ** Libc functions malloc(3) 20 | */ 21 | 22 | #include "libft.h" 23 | 24 | void *ft_memalloc(size_t size) 25 | { 26 | void *mem; 27 | 28 | mem = malloc(size); 29 | if (!mem) 30 | return (NULL); 31 | ft_bzero(mem, size); 32 | return (mem); 33 | } 34 | 35 | /* 36 | ** int main() 37 | ** { 38 | ** char *result = ft_memalloc(5); 39 | ** printf("The result is %s\n", result); 40 | ** return 0; 41 | ** } 42 | */ 43 | -------------------------------------------------------------------------------- /ft_bzero.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_bzero.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/14 13:53:28 by obibik #+# #+# */ 9 | /* Updated: 2018/08/14 13:53:38 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** bzero -- write zeroes to a byte string 15 | ** The bzero() function writes n zeroed bytes to the string s. 16 | ** If n is zero, bzero() does nothing. 17 | */ 18 | 19 | #include "libft.h" 20 | 21 | void ft_bzero(void *s, size_t n) 22 | { 23 | char *tmp; 24 | 25 | tmp = (char *)s; 26 | while (n > 0) 27 | { 28 | *tmp = 0; 29 | tmp++; 30 | n--; 31 | } 32 | return ; 33 | } 34 | 35 | /* 36 | ** void ft_bzero(void *s, size_t n) 37 | ** { 38 | ** ft_memset(s, 0, n); 39 | ** } 40 | */ 41 | 42 | /* 43 | ** int main() 44 | ** { 45 | ** char str[100] = "Libc is the standard library."; 46 | ** 47 | ** ft_bzero(str + 3, 1); 48 | ** printf("After memset(): %s\n", str); 49 | ** return 0; 50 | ** } 51 | */ 52 | -------------------------------------------------------------------------------- /ft_strnew.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strnew.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/22 16:59:44 by obibik #+# #+# */ 9 | /* Updated: 2018/08/22 16:59:47 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** Allocates (with malloc(3)) and returns a “fresh” string ending 15 | ** with ’\0’. Each character of the string is initialized at 16 | ** ’\0’. If the allocation fails the function returns NULL. 17 | ** Param. #1 The size of the string to be allocated. 18 | ** Return value The string allocated and initialized to 0. 19 | ** Libc functions malloc(3) 20 | ** 21 | ** 22 | ** we have to add + 1 to our size_t to compensate for the terminating '\0'. 23 | */ 24 | 25 | #include "libft.h" 26 | 27 | char *ft_strnew(size_t size) 28 | { 29 | return ((char *)ft_memalloc(size + 1)); 30 | } 31 | 32 | /* 33 | ** int main() 34 | ** { 35 | ** char *result = ft_strnew(5); 36 | ** printf("The result is %s\n", result); 37 | ** return 0; 38 | ** } 39 | */ 40 | -------------------------------------------------------------------------------- /ft_putnbr_fd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putnbr_fd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/22 16:56:57 by obibik #+# #+# */ 9 | /* Updated: 2018/08/22 16:56:59 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** Description Outputs the integer n to the file descriptor fd. 15 | ** Param. #1 The integer to print. 16 | ** Param. #2 The file descriptor. 17 | ** Return value None. 18 | ** Libc functions write(2). 19 | */ 20 | 21 | #include "libft.h" 22 | 23 | void ft_putnbr_fd(int n, int fd) 24 | { 25 | if (n == -2147483648) 26 | ft_putstr_fd("-2147483648", fd); 27 | else if (n < 0) 28 | { 29 | ft_putchar_fd('-', fd); 30 | ft_putnbr_fd(-n, fd); 31 | } 32 | else if (n >= 10) 33 | { 34 | ft_putnbr_fd(n / 10, fd); 35 | ft_putchar_fd(n % 10 + '0', fd); 36 | } 37 | else 38 | ft_putchar_fd(n + '0', fd); 39 | } 40 | 41 | /* 42 | ** int main() 43 | ** { 44 | ** int nmb = 432; 45 | ** ft_putnbr_fd(nmb, 1); 46 | ** return (0); 47 | ** } 48 | */ 49 | -------------------------------------------------------------------------------- /ft_strcmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strcmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/14 17:34:23 by obibik #+# #+# */ 9 | /* Updated: 2018/08/14 17:34:25 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** strcmp, strncmp -- compare strings 15 | */ 16 | 17 | #include "libft.h" 18 | 19 | int ft_strcmp(const char *s1, const char *s2) 20 | { 21 | int i; 22 | 23 | i = 0; 24 | while (s1[i] == s2[i] && s1[i] != '\0' && s2[i] != '\0') 25 | i++; 26 | return ((unsigned char)s1[i] - (unsigned char)s2[i]); 27 | } 28 | 29 | /* 30 | ** int main () { 31 | ** char str1[15] = "abcdef"; 32 | ** char str2[15] = "ABCDEF"; 33 | ** int result = ft_strcmp(str1, str2); 34 | ** printf("%d\n", result); 35 | ** if(result < 0) { 36 | ** printf("str1 is less than str2"); 37 | ** } else if(result > 0) { 38 | ** printf("str2 is less than str1"); 39 | ** } else { 40 | ** printf("str1 is equal to str2"); 41 | ** } 42 | ** 43 | ** return(0); 44 | ** } 45 | */ 46 | -------------------------------------------------------------------------------- /ft_strequ.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strequ.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/22 16:58:09 by obibik #+# #+# */ 9 | /* Updated: 2018/08/22 16:58:11 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** Lexicographical comparison between s1 and s2. If the 2 15 | ** strings are identical the function returns 1, or 0 otherwise. 16 | ** Param. #1 The first string to be compared. 17 | ** Param. #2 The second string to be compared. 18 | ** Return value 1 or 0 according to if the 2 strings are identical or not. 19 | ** Libc functions None. 20 | */ 21 | 22 | #include "libft.h" 23 | 24 | int ft_strequ(char const *s1, char const *s2) 25 | { 26 | if (!s1 || !s2) 27 | return (-1); 28 | return (ft_strcmp(s1, s2) ? 0 : 1); 29 | } 30 | 31 | /* 32 | ** int main() 33 | ** { 34 | ** char s1[10] = "String 2."; 35 | ** char s2[10] = "String 2."; 36 | ** int result = ft_strequ(s1, s2); 37 | ** printf("The result is %d\n", result); 38 | ** return 0; 39 | ** } 40 | */ 41 | -------------------------------------------------------------------------------- /ft_strlcat.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strlcat.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/14 17:32:56 by obibik #+# #+# */ 9 | /* Updated: 2018/08/14 17:32:59 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** strlcpy, strlcat -- size-bounded string copying and concatenation 15 | */ 16 | 17 | #include "libft.h" 18 | 19 | size_t ft_strlcat(char *dst, const char *src, size_t size) 20 | { 21 | size_t i; 22 | size_t len; 23 | 24 | len = ft_strlen(dst) + ft_strlen(src); 25 | if (size <= ft_strlen(dst)) 26 | return (ft_strlen(src) + size); 27 | while (*dst) 28 | dst++; 29 | i = 0; 30 | while ((i < size - (len - ft_strlen(src)) - 1) && src[i]) 31 | { 32 | dst[i] = src[i]; 33 | i++; 34 | } 35 | dst[i] = '\0'; 36 | return (len); 37 | } 38 | 39 | /* 40 | ** int main () { 41 | ** char s1[50] = "This is s1. "; 42 | ** char s2[50] = "This is s2."; 43 | ** 44 | ** ft_strlcat(s1, s2, 3); 45 | ** printf("Final string : %s\n", s1); 46 | ** return(0); 47 | ** } 48 | */ 49 | -------------------------------------------------------------------------------- /ft_lstmap.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstmap.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/22 17:47:02 by obibik #+# #+# */ 9 | /* Updated: 2018/08/22 17:47:05 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** Description Iterates a list lst and applies the function f to each link to 15 | ** create a “fresh” list (using malloc(3)) resulting from the successive 16 | ** applications of f. If the allocation fails, the function 17 | ** returns NULL. 18 | ** Param. #1 A pointer’s to the first link of a list. 19 | ** Param. #2 The address of a function to apply to each link of a list. 20 | ** Return value The new list. 21 | ** Libc functions malloc(3), free(3). 22 | */ 23 | 24 | #include "libft.h" 25 | 26 | t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)) 27 | { 28 | t_list *newlist; 29 | 30 | if (lst != NULL && f != NULL) 31 | { 32 | newlist = f(lst); 33 | if (newlist != NULL && lst->next != NULL) 34 | newlist->next = ft_lstmap(lst->next, f); 35 | return (newlist); 36 | } 37 | return (NULL); 38 | } 39 | -------------------------------------------------------------------------------- /ft_lstdelone.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstdelone.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/22 17:46:03 by obibik #+# #+# */ 9 | /* Updated: 2018/08/22 17:46:04 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** Takes as a parameter a link’s pointer address and frees the 15 | ** memory of the link’s content using the function del given as 16 | ** a parameter, then frees the link’s memory using free(3). The 17 | ** memory of next must not be freed under any circumstance. 18 | ** Finally, the pointer to the link that was just freed must be 19 | ** set to NULL (quite similar to the function ft_memdel in the 20 | ** mandatory part). 21 | ** Param. #1 The adress of a pointer to a link that needs to be freed. 22 | ** Return value None. 23 | ** Libc functions free(3) 24 | */ 25 | 26 | #include "libft.h" 27 | 28 | void ft_lstdelone(t_list **alst, void (*del)(void *, size_t)) 29 | { 30 | if (del != NULL && alst != NULL) 31 | { 32 | del((**alst).content, (**alst).content_size); 33 | free(*alst); 34 | *alst = NULL; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /ft_strrchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strrchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/14 17:33:29 by obibik #+# #+# */ 9 | /* Updated: 2018/08/14 17:33:32 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** strchr, strrchr -- locate character in string 15 | ** The strrchr() function is identical to strchr(), 16 | ** except it locates the last occurrence of c. 17 | */ 18 | 19 | #include "libft.h" 20 | 21 | char *ft_strrchr(const char *s, int c) 22 | { 23 | int i; 24 | int k; 25 | 26 | i = 0; 27 | k = -1; 28 | while (s[i] != '\0') 29 | { 30 | if (s[i] == c) 31 | k = i; 32 | i++; 33 | } 34 | if (c == '\0') 35 | return ((char *)&s[i]); 36 | if (k == -1) 37 | return (0); 38 | return ((char *)&s[k]); 39 | } 40 | 41 | /* 42 | ** int main () { 43 | ** const char str[] = "http://www.tutorialspoint.com"; 44 | ** const char ch = '.'; 45 | ** char *result; 46 | ** 47 | ** result = ft_strrchr(str, ch); 48 | ** printf("String after a character is %s\n", result); 49 | ** return(0); 50 | ** } 51 | */ 52 | -------------------------------------------------------------------------------- /ft_atoi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_atoi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/14 17:34:48 by obibik #+# #+# */ 9 | /* Updated: 2018/08/14 17:34:50 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** atoi, atoi_l -- convert ASCII string to integer 15 | */ 16 | 17 | #include "libft.h" 18 | 19 | int ft_atoi(const char *str) 20 | { 21 | int result; 22 | int i; 23 | int sign; 24 | 25 | result = 0; 26 | i = 0; 27 | sign = 1; 28 | while (str[i] == ' ' || str[i] == '\n' || str[i] == '\r' || 29 | str[i] == '\t' || str[i] == '\v' || str[i] == '\f') 30 | i++; 31 | if (str[i] == '-' || str[i] == '+') 32 | { 33 | if (str[i] == '-') 34 | sign = -1; 35 | i++; 36 | } 37 | while (str[i] != '\0' && str[i] >= '0' && str[i] <= '9') 38 | { 39 | result = result * 10 + str[i] - '0'; 40 | i++; 41 | } 42 | return (sign * result); 43 | } 44 | 45 | /* 46 | ** int main() 47 | ** { 48 | ** char str[] = "-i89789"; 49 | ** int val = ft_atoi(str); 50 | ** printf ("%d ", val); 51 | ** return 0; 52 | ** } 53 | */ 54 | -------------------------------------------------------------------------------- /ft_lstdel.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstdel.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/22 17:46:17 by obibik #+# #+# */ 9 | /* Updated: 2018/08/22 17:46:20 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** Takes as a parameter the adress of a pointer to a link and 15 | ** frees the memory of this link and every successors of that link 16 | ** using the functions del and free(3). Finally the pointer to 17 | ** the link that was just freed must be set to NULL (quite similar 18 | ** to the function ft_memdel from the mandatory part). 19 | ** Param. #1 The address of a pointer to the first link of a list that needs 20 | ** to be freed. 21 | ** Return value None. 22 | ** Libc functions free(3) 23 | */ 24 | 25 | #include "libft.h" 26 | 27 | void ft_lstdel(t_list **alst, void (*del)(void *, size_t)) 28 | { 29 | t_list *tmp; 30 | t_list *next; 31 | 32 | tmp = *alst; 33 | if (del != NULL) 34 | { 35 | while (tmp != NULL) 36 | { 37 | next = tmp->next; 38 | del(tmp->content, tmp->content_size); 39 | free(tmp); 40 | tmp = next; 41 | } 42 | *alst = NULL; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /ft_memcpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/14 15:04:24 by obibik #+# #+# */ 9 | /* Updated: 2018/08/14 16:14:37 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** memcpy -- copy memory area 15 | ** The memcpy() function copies n bytes from memory area src 16 | ** to memory area dst. If dst and src overlap, behavior is undefined. 17 | ** Applications in which dst and src might overlap should use 18 | ** memmove(3) instead. The memcpy() function returns the original value of dst. 19 | */ 20 | 21 | #include "libft.h" 22 | 23 | void *ft_memcpy(void *dst, const void *src, size_t n) 24 | { 25 | size_t i; 26 | char *d; 27 | char *s; 28 | 29 | i = 0; 30 | d = (char *)dst; 31 | s = (char *)src; 32 | while (i < n) 33 | { 34 | d[i] = s[i]; 35 | i++; 36 | } 37 | return (dst); 38 | } 39 | 40 | /* 41 | ** int main() 42 | ** { 43 | ** char dst[100] = "Libc is"; 44 | ** char src[100] = "the standard library."; 45 | ** 46 | ** ft_memcpy(dst, src, sizeof(src)); 47 | ** printf("dst after memset(): %s\n", dst); 48 | ** return 0; 49 | ** } 50 | */ 51 | -------------------------------------------------------------------------------- /ft_memcmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memcmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/14 17:29:35 by obibik #+# #+# */ 9 | /* Updated: 2018/08/14 17:29:37 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** memcmp -- compare byte string 15 | */ 16 | 17 | #include "libft.h" 18 | 19 | int ft_memcmp(const void *s1, const void *s2, size_t n) 20 | { 21 | unsigned char *s1c; 22 | unsigned char *s2c; 23 | size_t i; 24 | 25 | i = -1; 26 | s1c = (unsigned char *)s1; 27 | s2c = (unsigned char *)s2; 28 | while (++i < n && *(s1c + i) == *(s2c + i)) 29 | ; 30 | if (i == n) 31 | return (0); 32 | return (*(s1c + i) - *(s2c + i)); 33 | } 34 | 35 | /* 36 | ** int main () { 37 | ** char str1[15] = "abcdef"; 38 | ** char str2[15] = "ABCDEF"; 39 | ** int result; 40 | ** 41 | ** result = memcmp(str1, str2, 5); 42 | ** if(result > 0) { 43 | ** printf("str2 is less than str1"); 44 | ** } else if(result < 0) { 45 | ** printf("str1 is less than str2"); 46 | ** } else { 47 | ** printf("str1 is equal to str2"); 48 | ** } 49 | ** return(0); 50 | ** } 51 | */ 52 | -------------------------------------------------------------------------------- /ft_strncmp.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strncmp.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/14 17:34:36 by obibik #+# #+# */ 9 | /* Updated: 2018/08/14 17:34:38 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** strcmp, strncmp -- compare strings 15 | */ 16 | 17 | #include "libft.h" 18 | 19 | int ft_strncmp(const char *s1, const char *s2, size_t n) 20 | { 21 | while (*s1 && *s2 && *s1 == *s2 && (int)n > 0) 22 | { 23 | s1++; 24 | s2++; 25 | n--; 26 | } 27 | if (n) 28 | return (*(unsigned char *)s1 - *(unsigned char *)s2); 29 | else 30 | return (0); 31 | } 32 | 33 | /* 34 | ** int main () { 35 | ** char str1[15] = "ABCdef"; 36 | ** char str2[15] = "ABCDEF"; 37 | ** int result; 38 | ** 39 | ** result = ft_strncmp(str1, str2, 3); 40 | ** if(result < 0) { 41 | ** printf("str1 is less than str2\n"); 42 | ** } else if(result > 0) { 43 | ** printf("str2 is less than str1\n"); 44 | ** } else { 45 | ** printf("str1 is equal to str2\n"); 46 | ** } 47 | ** printf("Value returned by strncmp() is: %d\n", result); 48 | ** return(0); 49 | ** } 50 | */ 51 | -------------------------------------------------------------------------------- /ft_striter.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_striter.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/22 16:58:23 by obibik #+# #+# */ 9 | /* Updated: 2018/08/22 16:58:25 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** Applies the function f to each character of the string passed 15 | ** as argument. Each character is passed by address to f to be 16 | ** modified if necessary. 17 | ** Param. #1 The string to iterate. 18 | ** Param. #2 The function to apply to each character of s. 19 | ** Return value None. 20 | ** Libc functions None. 21 | */ 22 | 23 | #include "libft.h" 24 | 25 | void ft_striter(char *s, void (*f)(char *)) 26 | { 27 | unsigned int i; 28 | 29 | if (!s || !f) 30 | return ; 31 | i = 0; 32 | while (s[i] != '\0') 33 | { 34 | f(s + i); 35 | i++; 36 | } 37 | } 38 | 39 | /* 40 | ** void my_func(char *str) 41 | ** { 42 | ** printf("My inner function %s\n", str); 43 | ** } 44 | ** 45 | ** int main() 46 | ** { 47 | ** char str[10] = "Hello."; 48 | ** printf("The result is %s\n", str); 49 | ** ft_striter(str, my_func); 50 | ** printf("The result is %s\n", str); 51 | ** return 0; 52 | ** } 53 | */ 54 | -------------------------------------------------------------------------------- /ft_strnequ.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strnequ.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/22 16:59:34 by obibik #+# #+# */ 9 | /* Updated: 2018/08/22 16:59:36 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** Description Lexicographical comparison between s1 and s2 up to n characters 15 | ** or until a ’\0’ is reached. If the 2 strings are identical, 16 | ** the function returns 1, or 0 otherwise. 17 | ** Param. #1 The first string to be compared. 18 | ** Param. #2 The second string to be compared. 19 | ** Param. #3 The maximum number of characters to be compared. 20 | ** Return value 1 or 0 according to if the 2 strings are identical or not. 21 | ** Libc functions None. 22 | */ 23 | 24 | #include "libft.h" 25 | 26 | int ft_strnequ(char const *s1, char const *s2, size_t n) 27 | { 28 | if (!s1 || !s2) 29 | return (-1); 30 | return (ft_strncmp(s1, s2, n) ? 0 : 1); 31 | } 32 | 33 | /* 34 | ** int main() 35 | ** { 36 | ** char s1[20] = "String 22222."; 37 | ** char s2[10] = "String 2."; 38 | ** int result = ft_strnequ(s1, s2, 3); 39 | ** printf("The result is %d\n", result); 40 | ** return 0; 41 | ** } 42 | */ 43 | -------------------------------------------------------------------------------- /ft_strstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/14 17:33:45 by obibik #+# #+# */ 9 | /* Updated: 2018/08/14 17:33:48 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** strstr, strcasestr, strnstr -- locate a substring in a string 15 | ** The function finds the first occurrence of the substring needle 16 | ** in the string haystack. 17 | */ 18 | 19 | #include "libft.h" 20 | 21 | char *ft_strstr(const char *haystack, const char *needle) 22 | { 23 | int i; 24 | int j; 25 | 26 | i = 0; 27 | if (!*needle) 28 | return ((char *)haystack); 29 | while (haystack[i]) 30 | { 31 | j = 0; 32 | while (haystack[i] == needle[j] && haystack[i]) 33 | { 34 | i++; 35 | j++; 36 | } 37 | if (!needle[j]) 38 | return ((char *)&haystack[i - j]); 39 | i = (i - j) + 1; 40 | } 41 | return (NULL); 42 | } 43 | 44 | /* 45 | ** int main () { 46 | ** const char haystack[20] = "TutorialsPoint"; 47 | ** const char needle[10] = "Tu"; 48 | ** char *result; 49 | ** 50 | ** result = ft_strstr(haystack, needle); 51 | ** printf("The substring is: %s\n", result); 52 | ** return(0); 53 | ** } 54 | */ 55 | -------------------------------------------------------------------------------- /ft_strchr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strchr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/14 17:33:14 by obibik #+# #+# */ 9 | /* Updated: 2018/08/14 17:33:16 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** strchr, strrchr -- locate character in string 15 | ** The strchr() function locates the first occurrence of c (converted to a char) 16 | ** in the string pointed to by s. The terminating null character is considered 17 | ** to be part of the string; therefore if c is `\0', 18 | ** the functions locate the terminating `\0'. 19 | ** The functions strchr() and strrchr() return a pointer to the located 20 | ** character, or NULL if the character does not appear in the string. 21 | */ 22 | 23 | #include "libft.h" 24 | 25 | char *ft_strchr(const char *s, int c) 26 | { 27 | while (*s != (char)c) 28 | { 29 | if (!*s++) 30 | return (0); 31 | } 32 | return (char *)s; 33 | } 34 | 35 | /* 36 | ** int main () { 37 | ** const char str[] = "http://www.tutorialspoint.com"; 38 | ** const char ch = 'f'; 39 | ** char *result; 40 | ** 41 | ** result = ft_strchr(str, ch); 42 | ** printf("String after a character is %s\n", result); 43 | ** return(0); 44 | ** } 45 | */ 46 | -------------------------------------------------------------------------------- /ft_lstadd.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstadd.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/22 17:46:32 by obibik #+# #+# */ 9 | /* Updated: 2018/08/22 17:46:35 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** Adds the element new at the beginning of the list. 15 | ** Param. #1 The address of a pointer to the first link of a list. 16 | ** Param. #2 The link to add at the beginning of the list. 17 | ** Return value None. 18 | ** Libc functions None. 19 | */ 20 | 21 | #include "libft.h" 22 | 23 | void ft_lstadd(t_list **alst, t_list *new) 24 | { 25 | if (new != NULL) 26 | { 27 | new->next = *alst; 28 | *alst = new; 29 | } 30 | } 31 | 32 | /* 33 | ** int main() 34 | ** { 35 | ** t_list *my_tlist = (t_list *)malloc(sizeof(t_list) * 5); 36 | ** char content[20] = "CONTENT."; 37 | ** t_list *linked_list_item1 = ft_lstnew((void *)content, 16); 38 | ** char content2[20] = "CONTENT2."; 39 | ** t_list *linked_list_item2 = ft_lstnew((void *)content2, 16); 40 | ** ft_lstadd(&my_tlist, linked_list_item1); 41 | ** ft_lstadd(&my_tlist, linked_list_item2); 42 | ** printf("The result is %s\n", (char *)my_tlist[0].content); 43 | ** printf("The result is %s\n", (char *)my_tlist[0].next->content); 44 | ** return 0; 45 | ** } 46 | */ 47 | -------------------------------------------------------------------------------- /ft_striteri.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_striteri.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/22 16:58:34 by obibik #+# #+# */ 9 | /* Updated: 2018/08/22 16:58:37 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** Description Applies the function f to each character of the string passed 15 | ** as argument, and passing its index as first argument. Each 16 | ** character is passed by address to f to be modified if necessary. 17 | ** Param. #1 The string to iterate. 18 | ** Param. #2 The function to apply to each character of s and its index. 19 | ** Return value None. 20 | ** Libc functions None. 21 | */ 22 | 23 | #include "libft.h" 24 | 25 | void ft_striteri(char *s, void (*f)(unsigned int, char *)) 26 | { 27 | unsigned int i; 28 | 29 | if (!s || !f) 30 | return ; 31 | i = 0; 32 | while (s[i] != '\0') 33 | { 34 | f(i, s + i); 35 | i++; 36 | } 37 | } 38 | 39 | /* 40 | ** void my_func(unsigned int i, char *str) 41 | ** { 42 | ** printf("My inner function: index = %d and the string is %s\n", i, str); 43 | ** } 44 | ** 45 | ** int main() 46 | ** { 47 | ** char str[10] = "Hello."; 48 | ** printf("The result is %s\n", str); 49 | ** ft_striteri(str, my_func); 50 | ** printf("The result is %s\n", str); 51 | ** return 0; 52 | ** } 53 | */ 54 | -------------------------------------------------------------------------------- /ft_memmove.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memmove.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/14 17:20:28 by obibik #+# #+# */ 9 | /* Updated: 2018/08/14 17:20:33 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** memmove -- copy byte string 15 | ** The memmove() function copies len bytes from string src to string dst. 16 | ** The two strings may overlap; the copy is always done in a non-destructive 17 | ** manner. The memmove() function returns the original value of dst. 18 | */ 19 | 20 | #include "libft.h" 21 | 22 | void *ft_memmove(void *dst, const void *src, size_t len) 23 | { 24 | char *d; 25 | char *s; 26 | size_t i; 27 | 28 | d = (char*)dst; 29 | s = (char*)src; 30 | i = 0; 31 | if (d == s) 32 | return (d); 33 | if (s < d) 34 | { 35 | i = len; 36 | while (i--) 37 | ((char*)d)[i] = ((char*)s)[i]; 38 | } 39 | else 40 | { 41 | i = 0; 42 | while (i < len) 43 | { 44 | ((char*)d)[i] = ((char*)s)[i]; 45 | i++; 46 | } 47 | } 48 | return (d); 49 | } 50 | 51 | /* 52 | ** void * memmove(void *s1, const void *s2, size_t n) 53 | ** { 54 | ** return memcpy(s1, s2, n); 55 | ** } 56 | */ 57 | 58 | /* 59 | ** int main() 60 | ** { 61 | ** char dst[100] = "Libc is"; 62 | ** char src[100] = "the standard library."; 63 | ** ft_memmove(dst, src, 5); 64 | ** printf("dst after memset(): %s\n", dst); 65 | ** return 0; 66 | ** } 67 | */ 68 | -------------------------------------------------------------------------------- /ft_strsub.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strsub.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/22 17:00:22 by obibik #+# #+# */ 9 | /* Updated: 2018/08/22 17:00:24 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** Allocates (with malloc(3)) and returns a “fresh” substring 15 | ** from the string given as argument. The substring begins at 16 | ** indexstart and is of size len. If start and len aren’t refering 17 | ** to a valid substring, the behavior is undefined. If the 18 | ** allocation fails, the function returns NULL. 19 | ** Param. #1 The string from which create the substring. 20 | ** Param. #2 The start index of the substring. 21 | ** Param. #3 The size of the substring. 22 | ** Return value The substring. 23 | ** Libc functions malloc(3) 24 | */ 25 | 26 | #include "libft.h" 27 | 28 | char *ft_strsub(char const *s, unsigned int start, size_t len) 29 | { 30 | unsigned int i; 31 | char *str; 32 | 33 | if (!s) 34 | return (NULL); 35 | i = 0; 36 | str = (char *)malloc(sizeof(char) * len + 1); 37 | if (str == NULL) 38 | return (NULL); 39 | while (i < len) 40 | { 41 | str[i] = s[start + i]; 42 | i++; 43 | } 44 | str[i] = '\0'; 45 | return (str); 46 | } 47 | 48 | /* 49 | ** int main() 50 | ** { 51 | ** char str[20] = "This is string."; 52 | ** char *result = ft_strsub(str, 9, 2); 53 | ** printf("The result is %s\n", result); 54 | ** return 0; 55 | ** } 56 | */ 57 | -------------------------------------------------------------------------------- /ft_memccpy.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_memccpy.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/14 16:18:15 by obibik #+# #+# */ 9 | /* Updated: 2018/08/14 16:22:16 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** memccpy -- copy string until character found 15 | ** The memccpy() function copies bytes from string src to string dst. 16 | ** If the character c (as converted to an unsigned char) occurs 17 | ** in the string src, the copy stops and a pointer to the byte after 18 | ** the copy of c in the string dst is returned. Otherwise, n bytes are copied, 19 | ** and a NULL pointer is returned. The source and destination strings 20 | ** should not overlap, as the behavior is undefined. 21 | */ 22 | 23 | #include "libft.h" 24 | 25 | void *ft_memccpy(void *dst, const void *src, int c, size_t n) 26 | { 27 | size_t i; 28 | char *dst2; 29 | char *src2; 30 | 31 | i = 0; 32 | dst2 = (char *)dst; 33 | src2 = (char *)src; 34 | while (i < n) 35 | { 36 | dst2[i] = src2[i]; 37 | if ((unsigned char)src2[i] == (unsigned char)c) 38 | return ((char *)dst + i + 1); 39 | i++; 40 | } 41 | return (NULL); 42 | } 43 | 44 | /* 45 | ** int main() 46 | ** { 47 | ** char dst[100] = "Libc is"; 48 | ** char src[100] = "the standard library."; 49 | ** char c = 'e'; 50 | ** 51 | ** ft_memccpy(dst, src, c, sizeof(src)); 52 | ** printf("dst after memccpy(): %s\n", dst); 53 | ** return 0; 54 | ** } 55 | */ 56 | -------------------------------------------------------------------------------- /ft_strmap.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strmap.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/22 16:59:05 by obibik #+# #+# */ 9 | /* Updated: 2018/08/22 16:59:08 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** Applies the function f to each character of the string given 15 | ** as argument to create a “fresh” new string (with malloc(3)) 16 | ** resulting from the successive applications of f. 17 | ** Param. #1 The string to map. 18 | ** Param. #2 The function to apply to each character of s. 19 | ** Return value The “fresh” string created from the successive applications of 20 | ** f. 21 | ** Libc functions malloc(3) 22 | */ 23 | 24 | #include "libft.h" 25 | 26 | char *ft_strmap(char const *s, char (*f)(char)) 27 | { 28 | unsigned int i; 29 | char *str; 30 | 31 | if (!s) 32 | return (NULL); 33 | i = 0; 34 | str = (char *)malloc(sizeof(char) * (ft_strlen(s)) + 1); 35 | if (str == NULL) 36 | return (NULL); 37 | while (s[i] != '\0') 38 | { 39 | str[i] = f(s[i]); 40 | i++; 41 | } 42 | str[i] = '\0'; 43 | return (str); 44 | } 45 | 46 | /* 47 | ** char my_func(char str) 48 | ** { 49 | ** printf("My inner function %c\n", str); 50 | ** return str; 51 | ** } 52 | ** 53 | ** int main() 54 | ** { 55 | ** char str[10] = "Hello."; 56 | ** printf("The result is %s\n", str); 57 | ** ft_strmap(str, my_func); 58 | ** printf("The result is %s\n", str); 59 | ** return 0; 60 | ** } 61 | */ 62 | -------------------------------------------------------------------------------- /ft_strjoin.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strjoin.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/22 16:58:52 by obibik #+# #+# */ 9 | /* Updated: 2018/08/22 16:58:54 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** Allocates (with malloc(3)) and returns a “fresh” string ending 15 | ** with ’\0’, result of the concatenation of s1 and s2. If 16 | ** the allocation fails the function returns NULL. 17 | ** Param. #1 The prefix string. 18 | ** Param. #2 The suffix string. 19 | ** Return value The “fresh” string result of the concatenation of the 2 strings. 20 | ** Libc functions malloc(3) 21 | */ 22 | 23 | #include "libft.h" 24 | 25 | char *ft_strjoin(char const *s1, char const *s2) 26 | { 27 | int i; 28 | int j; 29 | char *str; 30 | 31 | if (!s1 || !s2) 32 | return (NULL); 33 | i = 0; 34 | j = 0; 35 | str = (char *)malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1)); 36 | if (str == NULL) 37 | return (NULL); 38 | while (s1[i] != '\0') 39 | { 40 | str[i] = s1[i]; 41 | i++; 42 | } 43 | while (s2[j] != '\0') 44 | { 45 | str[i + j] = s2[j]; 46 | j++; 47 | } 48 | str[i + j] = '\0'; 49 | return (str); 50 | } 51 | 52 | /* 53 | ** int main() 54 | ** { 55 | ** char s1[10] = "String 1."; 56 | ** char s2[10] = "String 2."; 57 | ** char *result = ft_strjoin(s1, s2); 58 | ** printf("The result is %s\n", result); 59 | ** printf("s1 doesn't change: %s\n", s1); 60 | ** printf("s2 doesn't change: %s\n", s2); 61 | ** return 0; 62 | ** } 63 | */ 64 | -------------------------------------------------------------------------------- /ft_strnstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strnstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/14 17:34:09 by obibik #+# #+# */ 9 | /* Updated: 2018/08/14 17:34:11 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** strstr, strcasestr, strnstr -- locate a substring in a string 15 | ** The strnstr() function locates the first occurrence of the null-terminated 16 | ** string needle in the string haystack, where not more than len characters 17 | ** are searched. Characters that appear after a `\0' character are not 18 | ** searched. Since the strnstr() function is a FreeBSD specific API, it 19 | ** should only be used when portability is not a concern. 20 | */ 21 | 22 | #include "libft.h" 23 | 24 | char *ft_strnstr(const char *haystack, const char *needle, size_t len) 25 | { 26 | unsigned long i; 27 | int j; 28 | 29 | j = 0; 30 | i = 0; 31 | if (!*needle) 32 | return ((char *)haystack); 33 | while (haystack[i]) 34 | { 35 | j = 0; 36 | while (haystack[i] == needle[j] && haystack[i] && i < len) 37 | { 38 | i++; 39 | j++; 40 | } 41 | if (!needle[j]) 42 | return ((char *)&haystack[i - j]); 43 | i = (i - j) + 1; 44 | } 45 | return (NULL); 46 | } 47 | 48 | /* 49 | ** int main () { 50 | ** const char haystack[20] = "TutorialsPoint"; 51 | ** const char needle[10] = "to"; 52 | ** char *result; 53 | ** 54 | ** result = ft_strnstr(haystack, needle, 4); 55 | ** printf("The substring is: %s\n", result); 56 | ** return(0); 57 | ** } 58 | */ 59 | -------------------------------------------------------------------------------- /ft_strmapi.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strmapi.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/22 16:59:23 by obibik #+# #+# */ 9 | /* Updated: 2018/08/22 16:59:25 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** Applies the function f to each character of the string passed 15 | ** as argument by giving its index as first argument to create a 16 | ** “fresh” new string (with malloc(3)) resulting from the successive 17 | ** applications of f. 18 | ** Param. #1 The string to map. 19 | ** Param. #2 The function to apply to each character of s and its index. 20 | ** Return value The “fresh” string created from the successive applications of 21 | ** f. 22 | ** Libc functions malloc(3) 23 | */ 24 | 25 | #include "libft.h" 26 | 27 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) 28 | { 29 | unsigned int i; 30 | char *str; 31 | 32 | if (!s) 33 | return (NULL); 34 | i = 0; 35 | str = (char *)malloc(sizeof(char) * (ft_strlen(s)) + 1); 36 | if (str == NULL) 37 | return (NULL); 38 | while (s[i] != '\0') 39 | { 40 | str[i] = f(i, s[i]); 41 | i++; 42 | } 43 | str[i] = '\0'; 44 | return (str); 45 | } 46 | 47 | /* 48 | ** char my_func(unsigned int i, char str) 49 | ** { 50 | ** printf("My inner function: index = %d and %c\n", i, str); 51 | ** return str - 32; 52 | ** } 53 | ** 54 | ** int main() 55 | ** { 56 | ** char str[10] = "hello."; 57 | ** printf("The result is %s\n", str); 58 | ** char *result = ft_strmapi(str, my_func); 59 | ** printf("The result is %s\n", result); 60 | ** return 0; 61 | ** } 62 | */ 63 | -------------------------------------------------------------------------------- /ft_itoa.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_itoa.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/22 16:52:24 by obibik #+# #+# */ 9 | /* Updated: 2018/08/22 16:54:51 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** itoa converts integer to string 15 | ** Allocate (with malloc(3)) and returns a “fresh” string ending 16 | ** with ’\0’ representing the integer n given as argument. 17 | ** Negative numbers must be supported. If the allocation fails, 18 | ** the function returns NULL. 19 | ** 20 | ** Param. #1 The integer to be transformed into a string. 21 | ** 22 | ** Return value: The string representing the integer passed as argument. 23 | ** Libc functions malloc(3) 24 | */ 25 | 26 | #include "libft.h" 27 | 28 | char *ft_itoa(int nbr) 29 | { 30 | int length; 31 | int sign; 32 | char *str; 33 | 34 | sign = nbr; 35 | length = 1; 36 | while (sign /= 10) 37 | length++; 38 | sign = nbr < 0 ? 1 : 0; 39 | length = nbr < 0 ? length += 1 : length; 40 | if (nbr == -2147483648) 41 | return (str = ft_strdup("-2147483648")); 42 | str = ft_strnew(length); 43 | if (!str) 44 | return (NULL); 45 | if (sign) 46 | str[0] = '-'; 47 | nbr = nbr < 0 ? nbr *= -1 : nbr; 48 | while (--length >= sign) 49 | { 50 | str[length] = (nbr >= 10) ? (nbr % 10) + 48 : nbr + 48; 51 | nbr /= 10; 52 | } 53 | str[ft_strlen(str)] = '\0'; 54 | return (str); 55 | } 56 | 57 | /* 58 | ** int main() 59 | ** { 60 | ** int nmb = 89; 61 | ** char *val = ft_itoa(nmb); 62 | ** printf("This is number %d\n", nmb); 63 | ** printf("This is string %s\n", val); 64 | ** return 0; 65 | ** } 66 | */ 67 | -------------------------------------------------------------------------------- /ft_strtrim.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strtrim.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/22 17:00:33 by obibik #+# #+# */ 9 | /* Updated: 2018/08/22 17:00:34 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** Allocates (with malloc(3)) and returns a copy of the string 15 | ** given as argument without whitespaces at the beginning or at 16 | ** the end of the string. Will be considered as whitespaces the 17 | ** following characters ’ ’, ’\n’ and ’\t’. If s has no whitespaces 18 | ** at the beginning or at the end, the function returns a 19 | ** copy of s. If the allocation fails the function returns NULL. 20 | ** Param. #1 The string to be trimed. 21 | ** Return value The “fresh” trimmed string or a copy of s. 22 | ** Libc functions malloc(3) 23 | */ 24 | 25 | #include "libft.h" 26 | 27 | char *ft_strtrim(char const *s) 28 | { 29 | unsigned int i; 30 | unsigned int j; 31 | unsigned int k; 32 | char *str; 33 | 34 | i = 0; 35 | k = 0; 36 | if (!s) 37 | return (NULL); 38 | while (s[i] == ' ' || s[i] == '\n' || s[i] == '\t') 39 | i++; 40 | if (s[i] == '\0') 41 | return (ft_strcpy(ft_memalloc(sizeof(char) * 2), "")); 42 | j = ft_strlen(s) - 1; 43 | while (s[j] == ' ' || s[j] == '\n' || s[j] == '\t') 44 | j--; 45 | if (!(str = (char *)malloc(sizeof(char) * (j - i + 2)))) 46 | return (NULL); 47 | while (k < j - i + 1) 48 | { 49 | str[k] = s[i + k]; 50 | k++; 51 | } 52 | str[k] = '\0'; 53 | return (str); 54 | } 55 | 56 | /* 57 | ** int main() 58 | ** { 59 | ** char s[30] = " \t\nThis is string. \tContinue."; 60 | ** char *result = ft_strtrim(s); 61 | ** printf("The result is %s\n", result); 62 | ** return 0; 63 | ** } 64 | */ 65 | -------------------------------------------------------------------------------- /ft_lstnew.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_lstnew.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/22 17:45:49 by obibik #+# #+# */ 9 | /* Updated: 2018/08/22 17:45:52 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** Description Allocates (with malloc(3)) and returns a “fresh” link. The 15 | ** variables content and content_size of the new link are initialized 16 | ** by copy of the parameters of the function. If the parameter 17 | ** content is nul, the variable content is initialized to 18 | ** NULL and the variable content_size is initialized to 0 even 19 | ** if the parameter content_size isn’t. The variable next is 20 | ** initialized to NULL. If the allocation fails, the function returns 21 | ** NULL. 22 | ** Param. #1 The content to put in the new link. 23 | ** Param. #2 The size of the content of the new link. 24 | ** Return value The new link. 25 | ** Libc functions malloc(3), free(3) 26 | */ 27 | 28 | #include "libft.h" 29 | 30 | t_list *ft_lstnew(void const *content, size_t content_size) 31 | { 32 | t_list *new; 33 | 34 | new = (t_list *)malloc(sizeof(t_list) * 1); 35 | if (new == NULL) 36 | return (NULL); 37 | if (content == NULL) 38 | { 39 | new->content = NULL; 40 | new->content_size = 0; 41 | } 42 | else 43 | { 44 | new->content = malloc(content_size); 45 | if (new->content == NULL) 46 | return (NULL); 47 | ft_memmove(new->content, content, content_size); 48 | new->content_size = content_size; 49 | } 50 | new->next = NULL; 51 | return (new); 52 | } 53 | 54 | /* 55 | ** int main() 56 | ** { 57 | ** char content[20] = "CONTENT."; 58 | ** t_list *result = ft_lstnew((void *)content, 16); 59 | ** printf("The content is %s", (char *)result->content); 60 | ** printf("The content size is %zu", result->content_size); 61 | ** return 0; 62 | ** } 63 | */ 64 | -------------------------------------------------------------------------------- /ft_strsplit.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_strsplit.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/22 17:00:07 by obibik #+# #+# */ 9 | /* Updated: 2018/08/22 17:00:12 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** Allocates (with malloc(3)) and returns an array of “fresh” 15 | ** strings (all ending with ’\0’, including the array itself) obtained 16 | ** by spliting s using the character c as a delimiter. 17 | ** If the allocation fails the function returns NULL. Example 18 | ** : ft_strsplit("*hello*fellow***students*", ’*’) returns 19 | ** the array ["hello", "fellow", "students"]. 20 | ** Param. #1 The string to split. 21 | ** Param. #2 The delimiter character. 22 | ** Return value The array of “fresh” strings result of the split. 23 | ** Libc functions malloc(3), free(3) 24 | */ 25 | 26 | #include "libft.h" 27 | 28 | static char *ft_strndup(const char *s, size_t n) 29 | { 30 | char *str; 31 | 32 | str = (char *)malloc(sizeof(char) * n + 1); 33 | if (str == NULL) 34 | return (NULL); 35 | str = ft_strncpy(str, s, n); 36 | str[n] = '\0'; 37 | return (str); 38 | } 39 | 40 | char **ft_strsplit(char const *s, char c) 41 | { 42 | int i; 43 | int j; 44 | int k; 45 | char **tab; 46 | 47 | i = 0; 48 | k = 0; 49 | if (!s || (!(tab = (char **)malloc(sizeof(char *) * 50 | (ft_cntwrd(s, c)) + 1)))) 51 | return (NULL); 52 | while (s[i]) 53 | { 54 | while (s[i] == c) 55 | i++; 56 | j = i; 57 | while (s[i] && s[i] != c) 58 | i++; 59 | if (i > j) 60 | { 61 | tab[k] = ft_strndup(s + j, i - j); 62 | k++; 63 | } 64 | } 65 | tab[k] = NULL; 66 | return (tab); 67 | } 68 | 69 | /* 70 | ** int main() 71 | ** { 72 | ** char str[20] = "This is string."; 73 | ** char **result = ft_strsplit(str, 'r'); 74 | ** printf("The result is %s\n", *result); 75 | ** printf("The result is %s\n", result[0]); 76 | ** printf("The result is %s\n", result[1]); 77 | ** return 0; 78 | ** } 79 | */ 80 | -------------------------------------------------------------------------------- /ft_putnbr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_putnbr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/22 16:56:26 by obibik #+# #+# */ 9 | /* Updated: 2018/08/22 16:56:29 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | /* 14 | ** Outputs the integer n to the standard output. 15 | ** Param. #1 The integer to output. 16 | ** Return value None. 17 | ** Libc functions write(2). 18 | ** 19 | ** 20 | ** We will be breaking down int number as a whole into individual 21 | ** single digit numbers and we will convert those int numbers into characters 22 | ** that we will display on the standard output using our ft_putchar function. 23 | ** 24 | ** We start by seeing if the int passed in parameter is -2147483648. The 25 | ** number 2147483648 is the largest number an int variable can possibly 26 | ** hold. When a negative sign is placed in front of it our function is not 27 | ** able to handle it. So we check to see if a negative version has been 28 | ** given and if so we just immediately return it. 29 | ** 30 | ** If -2147483648 has not been given we then check to see if another 31 | ** negative number has been given. If the number is negative we immediately 32 | ** use ft_putchar('-') to display the minus sign to the standard output. 33 | ** 34 | ** We check to see if the number is greater than or equal to 10. If it 35 | ** is we begin to break down the number recursively to each individual 36 | ** digit, where we will then convert it into a character. We use a + '0' 37 | ** to convert the number into a char value. 38 | */ 39 | 40 | #include "libft.h" 41 | 42 | void ft_putnbr(int n) 43 | { 44 | if (n == -2147483648) 45 | ft_putstr("-2147483648"); 46 | else if (n < 0) 47 | { 48 | ft_putchar('-'); 49 | ft_putnbr(-n); 50 | } 51 | else if (n >= 10) 52 | { 53 | ft_putnbr(n / 10); 54 | ft_putchar(n % 10 + '0'); 55 | } 56 | else 57 | ft_putchar(n + '0'); 58 | } 59 | 60 | /* 61 | ** int main() 62 | ** { 63 | ** int nmb = 432; 64 | ** ft_putnbr(nmb); 65 | ** return (0); 66 | ** } 67 | */ 68 | -------------------------------------------------------------------------------- /libft.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* libft.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: obibik +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/08/14 15:49:44 by obibik #+# #+# */ 9 | /* Updated: 2018/08/14 15:49:49 by obibik ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #ifndef LIBFT_H 14 | # define LIBFT_H 15 | 16 | # include 17 | # include 18 | # include 19 | 20 | typedef struct s_list 21 | { 22 | void *content; 23 | size_t content_size; 24 | struct s_list *next; 25 | } t_list; 26 | 27 | /* 28 | ** Libc functions 29 | */ 30 | 31 | void *ft_memset(void *b, int c, size_t len); 32 | void ft_bzero(void *s, size_t n); 33 | void *ft_memcpy(void *dst, const void *src, size_t n); 34 | void *ft_memccpy(void *dst, const void *src, 35 | int c, size_t n); 36 | void *ft_memmove(void *dst, const void *src, size_t len); 37 | void *ft_memchr(const void *s, int c, size_t n); 38 | int ft_memcmp(const void *s1, const void *s2, size_t n); 39 | 40 | size_t ft_strlen(const char *s); 41 | char *ft_strdup(const char *src); 42 | char *ft_strcpy(char *dst, const char *src); 43 | char *ft_strncpy(char *dest, const char *src, size_t len); 44 | char *ft_strcat(char *s1, const char *s2); 45 | char *ft_strncat(char *s1, const char *s2, size_t n); 46 | size_t ft_strlcat(char *dst, const char *src, size_t size); 47 | char *ft_strchr(const char *s, int c); 48 | char *ft_strrchr(const char *s, int c); 49 | char *ft_strstr(const char *haystack, const char *needle); 50 | char *ft_strnstr(const char *haystack, 51 | const char *needle, size_t len); 52 | int ft_strcmp(const char *s1, const char *s2); 53 | int ft_strncmp(const char *s1, const char *s2, size_t n); 54 | 55 | char *ft_itoa(int nbr); 56 | int ft_atoi(const char *str); 57 | 58 | int ft_isalpha(int c); 59 | int ft_isdigit(int c); 60 | int ft_isalnum(int c); 61 | int ft_isascii(int c); 62 | int ft_isprint(int c); 63 | int ft_toupper(int c); 64 | int ft_tolower(int c); 65 | 66 | /* 67 | ** Additional functions 68 | */ 69 | 70 | void *ft_memalloc(size_t size); 71 | void ft_memdel(void **ap); 72 | char *ft_strnew(size_t size); 73 | void ft_strdel(char **as); 74 | void ft_strclr(char *s); 75 | void ft_striter(char *s, void (*f)(char *)); 76 | void ft_striteri(char *s, void (*f)(unsigned int, char *)); 77 | char *ft_strmap(char const *s, char (*f)(char)); 78 | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); 79 | 80 | int ft_strequ(char const *s1, char const *s2); 81 | int ft_strnequ(char const *s1, char const *s2, size_t n); 82 | char *ft_strsub(char const *s, unsigned int start, size_t len); 83 | char *ft_strjoin(char const *s1, char const *s2); 84 | char *ft_strtrim(char const *s); 85 | char **ft_strsplit(char const *s, char c); 86 | 87 | void ft_putchar(char c); 88 | void ft_putstr(char const *s); 89 | void ft_putendl(char const *s); 90 | void ft_putnbr(int n); 91 | void ft_putchar_fd(char c, int fd); 92 | void ft_putstr_fd(char const *s, int fd); 93 | void ft_putendl_fd(char const *s, int fd); 94 | void ft_putnbr_fd(int n, int fd); 95 | 96 | /* 97 | ** Bonus functions 98 | */ 99 | 100 | t_list *ft_lstnew(void const *content, size_t content_size); 101 | void ft_lstdelone(t_list **alst, void (*del)(void *, size_t)); 102 | void ft_lstdel(t_list **alst, void (*del)(void *, size_t)); 103 | void ft_lstadd(t_list **alst, t_list *new); 104 | void ft_lstiter(t_list *lst, void (*f)(t_list *elem)); 105 | t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)); 106 | 107 | /* 108 | ** Extra functions 109 | */ 110 | int ft_cntwrd(char const *s, char c); 111 | #endif 112 | --------------------------------------------------------------------------------