diff options
Diffstat (limited to 'c07')
| -rw-r--r-- | c07/ex00/ft_strdup.c | 45 | ||||
| -rw-r--r-- | c07/ex01/ft_range.c | 28 | ||||
| -rw-r--r-- | c07/ex02/ft_ultimate_range.c | 34 | ||||
| -rw-r--r-- | c07/ex03/ft_strjoin.c | 72 | ||||
| -rw-r--r-- | c07/main.c | 61 |
5 files changed, 240 insertions, 0 deletions
diff --git a/c07/ex00/ft_strdup.c b/c07/ex00/ft_strdup.c new file mode 100644 index 0000000..c63bd27 --- /dev/null +++ b/c07/ex00/ft_strdup.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/07 15:39:50 by cacharle #+# #+# */ +/* Updated: 2019/07/08 07:49:52 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <stdlib.h> +#include <errno.h> + +int ft_strlen(char *str) +{ + int counter; + + counter = 0; + while (str[counter]) + counter++; + return (counter); +} + +char *ft_strdup(char *src) +{ + int i; + char *dup_ptr; + + dup_ptr = malloc(sizeof(char) * ft_strlen(src)); + if (dup_ptr == NULL) + { + errno = ENOMEM; + return (NULL); + } + i = 0; + while (src[i]) + { + dup_ptr[i] = src[i]; + i++; + } + dup_ptr[i] = '\0'; + return (dup_ptr); +} diff --git a/c07/ex01/ft_range.c b/c07/ex01/ft_range.c new file mode 100644 index 0000000..be171f3 --- /dev/null +++ b/c07/ex01/ft_range.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_range.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/07 16:03:47 by cacharle #+# #+# */ +/* Updated: 2019/07/08 08:03:28 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int *ft_range(int min, int max) +{ + int i; + int *range; + + if (min >= max) + return (NULL); + range = malloc(sizeof(int) * (max - min)); + i = 0; + while (i < max - min) + { + range[i] = min + i; + i++; + } + return (range); +} diff --git a/c07/ex02/ft_ultimate_range.c b/c07/ex02/ft_ultimate_range.c new file mode 100644 index 0000000..141889c --- /dev/null +++ b/c07/ex02/ft_ultimate_range.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ultimate_range.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/07 16:09:06 by cacharle #+# #+# */ +/* Updated: 2019/07/08 08:38:29 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <stdlib.h> + +int ft_ultimate_range(int **range, int min, int max) +{ + int i; + + if (min >= max) + { + *range = NULL; + return (0); + } + *range = (int*)malloc(sizeof(int) * (max - min)); + if (*range == NULL) + return (-1); + i = 0; + while (i < max - min) + { + (*range)[i] = min + i; + i++; + } + return (i); +} diff --git a/c07/ex03/ft_strjoin.c b/c07/ex03/ft_strjoin.c new file mode 100644 index 0000000..1264000 --- /dev/null +++ b/c07/ex03/ft_strjoin.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/07 16:14:25 by cacharle #+# #+# */ +/* Updated: 2019/07/08 11:08:00 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <stdlib.h> + +int my_strlen(char *str) +{ + int i; + + i = 0; + while(str[i]) + i++; + return (i); +} + +int cummulative_strlen(int size, char **strs) +{ + int i; + int len; + + len = 0; + i = 0; + while (size > 0) + { + i = 0; + while (strs[size - 1][i]) + { + i++; + len++; + } + size--; + } + return (len); +} + +char *ft_strjoin(int size, char **strs, char *sep) +{ + int i; + int j; + int k; + int l; + char *join; + + join = (char*)malloc(sizeof(char) * (cummulative_strlen(size, strs) + + my_strlen(sep) * (size - 1))); + j = 0; + l = 0; + while (l < size) + { + i = 0; + while (strs[l][i]) + { + join[j] = strs[l][i]; + i++; + j++; + } + k = 0; + while (l != size - 1 && sep[k]) + join[j++] = sep[k++]; + l++; + } + return (join); +} diff --git a/c07/main.c b/c07/main.c new file mode 100644 index 0000000..e967143 --- /dev/null +++ b/c07/main.c @@ -0,0 +1,61 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/08 08:19:02 by cacharle #+# #+# */ +/* Updated: 2019/07/08 11:00:55 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "ex00/ft_strdup.c" +#include "ex01/ft_range.c" +#include "ex02/ft_ultimate_range.c" +#include "ex03/ft_strjoin.c" +/*#include "ex04/ft_strdup.c"*/ +/*#include "ex05/ft_strdup.c"*/ + +int main() +{ + char *s = "bonjour"; + char *mydup = ft_strdup(s); + char *stdup = strdup(s); + + printf("%s\n", mydup); + printf("%s\n", stdup); + free(mydup); + free(stdup); + + printf("---------------\n"); + int *r = ft_range(-3, 5); + if (r) + for (int i = 0; i < 10; i++) + printf("%d ", r[i]); + printf("\n "); + free(r); + + printf("---------------\n"); + int *rult; + int len = ft_ultimate_range(&rult, -4, 15); + printf("len(%d): ", len); + for (int i = 0; i < len; i++) + printf("%d ", rult[i]); + printf("\n "); + + printf("---------------\n"); + char *strs[] = {"bonjour", "je", "suis", "charle"}; + char *sep = ", "; + int size = 4; + char *join; + /*printf("%d\n", cummulative_strlen(size, strs));*/ + join = ft_strjoin(size, strs, sep); + printf("%s\n", join); + free(join); + + return 0; +} |
