diff options
| author | Charles <sircharlesaze@gmail.com> | 2019-07-06 08:42:30 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2019-07-06 08:42:30 +0200 |
| commit | d732047d6681b1f64f7907d1c0413abdaab76050 (patch) | |
| tree | c2123d55a7431de9bb55371dcc78c955da607c6f /c03 | |
| parent | af8435d40cdb8e7871ff004fb21382c236f9bd0f (diff) | |
| download | piscine-d732047d6681b1f64f7907d1c0413abdaab76050.tar.gz piscine-d732047d6681b1f64f7907d1c0413abdaab76050.tar.bz2 piscine-d732047d6681b1f64f7907d1c0413abdaab76050.zip | |
c03 and begin c04
Diffstat (limited to 'c03')
| -rw-r--r-- | c03/ex00/ft_strcmp.c | 21 | ||||
| -rw-r--r-- | c03/ex01/ft_strncmp.c | 21 | ||||
| -rw-r--r-- | c03/ex02/ft_strcat.c | 28 | ||||
| -rw-r--r-- | c03/ex03/ft_strncat.c | 29 | ||||
| -rw-r--r-- | c03/ex04/ft_strstr.c | 48 | ||||
| -rw-r--r-- | c03/ex05/ft_strlcat.c | 44 | ||||
| -rw-r--r-- | c03/main.c | 73 |
7 files changed, 264 insertions, 0 deletions
diff --git a/c03/ex00/ft_strcmp.c b/c03/ex00/ft_strcmp.c new file mode 100644 index 0000000..9131204 --- /dev/null +++ b/c03/ex00/ft_strcmp.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/05 10:39:51 by cacharle #+# #+# */ +/* Updated: 2019/07/05 16:07:46 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_strcmp(char *s1, char *s2) +{ + while (*s1 == *s2 && *s1 && *s2) + { + s1++; + s2++; + } + return *s1 - *s2; +} diff --git a/c03/ex01/ft_strncmp.c b/c03/ex01/ft_strncmp.c new file mode 100644 index 0000000..1dc2639 --- /dev/null +++ b/c03/ex01/ft_strncmp.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/05 10:50:46 by cacharle #+# #+# */ +/* Updated: 2019/07/05 16:06:57 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_strncmp(char *s1, char *s2, unsigned int n) +{ + unsigned int i; + + i = 0; + while (s1[i] == s2[i] && s1[i] && s2[i] && i < n - 1) + i++; + return s1[i] - s2[i]; +} diff --git a/c03/ex02/ft_strcat.c b/c03/ex02/ft_strcat.c new file mode 100644 index 0000000..1ec10c0 --- /dev/null +++ b/c03/ex02/ft_strcat.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/05 11:52:56 by cacharle #+# #+# */ +/* Updated: 2019/07/05 12:58:36 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +char *ft_strcat(char *dest, char *src) +{ + char *dest_head; + + dest_head = dest; + while (*dest) + dest++; + while (*src) + { + *dest = *src; + dest++; + src++; + } + *dest = *src; + return dest_head; +} diff --git a/c03/ex03/ft_strncat.c b/c03/ex03/ft_strncat.c new file mode 100644 index 0000000..c28fb38 --- /dev/null +++ b/c03/ex03/ft_strncat.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/05 13:02:04 by cacharle #+# #+# */ +/* Updated: 2019/07/05 15:33:07 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +char *ft_strncat(char *dest, char *src, unsigned int nb) +{ + unsigned int i; + char *dest_end; + + i = 0; + while (dest[i] != '\0') + i++; + dest_end = dest + i; + i = 0; + while (i < nb && src[i] != '\0') + { + dest_end[i] = src[i]; + i++; + } + return (dest); +} diff --git a/c03/ex04/ft_strstr.c b/c03/ex04/ft_strstr.c new file mode 100644 index 0000000..e27ffc8 --- /dev/null +++ b/c03/ex04/ft_strstr.c @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/05 15:20:54 by cacharle #+# #+# */ +/* Updated: 2019/07/05 16:46:57 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#define MY_NULL 0x0 + +int ft_strlen(char *str) +{ + int counter; + + counter = 0; + while (*str != '\0') + { + counter++; + str++; + } + return (counter); +} + +char *ft_strstr(char *str, char *to_find) +{ + int i; + + if (!ft_strlen(to_find)) + return str; + while (*str) + { + i = 0; + while (to_find[i] && str[i]) + { + if (to_find[i] != str[i]) + break; + i++; + } + if (i == ft_strlen(to_find)) + return (str); + str++; + } + return (MY_NULL); +} diff --git a/c03/ex05/ft_strlcat.c b/c03/ex05/ft_strlcat.c new file mode 100644 index 0000000..5cacc4d --- /dev/null +++ b/c03/ex05/ft_strlcat.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/05 15:43:34 by cacharle #+# #+# */ +/* Updated: 2019/07/06 07:12:28 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +unsigned int my_strlen(char *str) +{ + int counter; + + counter = 0; + while (*str != '\0') + { + counter++; + str++; + } + return (counter); +} + +unsigned int ft_strlcat(char *dest, char *src, unsigned int size) +{ + unsigned int i; + unsigned int len_src; + unsigned int len_dest; + + len_src = my_strlen(src); + len_dest = my_strlen(dest); + if (!len_src) + return (len_dest); + i = len_dest - 1; + while (i < size - len_dest - 1 && src[i]) + { + dest[i] = src[i]; + i++; + } + dest[i] = '\0'; + return (len_src + len_dest); +} diff --git a/c03/main.c b/c03/main.c new file mode 100644 index 0000000..9c1add8 --- /dev/null +++ b/c03/main.c @@ -0,0 +1,73 @@ +#include <stdio.h> +#include <string.h> +#include "ex00/ft_strcmp.c" +#include "ex01/ft_strncmp.c" +#include "ex02/ft_strcat.c" +#include "ex03/ft_strncat.c" +#include "ex04/ft_strstr.c" +#include "ex05/ft_strlcat.c" + +int main() +{ + char s1[] = "bonjour"; + char s2[] = "bonjouj"; + printf("%d : %d\n", ft_strcmp(s1, s2), strcmp(s1, s2)); + + unsigned int size = 10; + printf("%d : %d\n", ft_strncmp(s1, s2, size), strncmp(s1, s2, size)); + + char *head; + char dest[10] = "abc"; + char src[] = "defg"; + head = ft_strcat(dest, src); + printf("\n%s ", head); + for (int i = 0; i < 15; i++) + printf("%d ", head[i]); + char _dest[10] = "abc"; + head = strcat(_dest, src); + printf("\n%s ", head); + for (int i = 0; i < 15; i++) + printf("%d ", head[i]); + + unsigned int nsize = 1; + char *nhead; + char ndest[10] = "abc"; + char nsrc[] = "defg"; + nhead = ft_strncat(ndest, nsrc, nsize); + printf("\n%s ", nhead); + for (int i = 0; i < 15; i++) + printf("%d ", nhead[i]); + char _ndest[10] = "abc"; + nhead = strncat(_ndest, nsrc, nsize); + printf("\n%s ", nhead); + for (int i = 0; i < 15; i++) + printf("%d ", nhead[i]); + + printf("\n"); + char *haystack = "abcdefg"; + char *needle = "abcdefg"; + char *found; + found = ft_strstr(haystack, needle); + if (found) + for (int i = 0; i < 5; i++) + printf("%d ", found[i]); + printf("\n%s\n", found); + found = strstr(haystack, needle); + if (found) + for (int i = 0; i < 5; i++) + printf("%d ", found[i]); + printf("\n%s\n", found); + + unsigned int lsize = 100; + char ldest[100] = "abcdef"; + char lsrc[] = "d"; + printf("\nsize %u, ", ft_strlcat(ldest, lsrc, lsize)); + printf("%s ", ldest); + for (int i = 0; i < 15; i++) + printf("%d ", ldest[i]); + char _ldest[100] = "abcdef"; + printf("\nsize %lu, ", strlcat(_ldest, lsrc, lsize)); + printf("%s ", ldest); + for (int i = 0; i < 15; i++) + printf("%d ", ldest[i]); +} |
