diff options
| -rw-r--r-- | c02/ex06/ft_str_is_printable.c | 4 | ||||
| -rw-r--r-- | c02/ex08/ft_strlowcase.c | 6 | ||||
| -rw-r--r-- | c02/ex10/ft_strlcpy.c | 6 | ||||
| -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 | ||||
| -rw-r--r-- | c04/ex00/ft_strlen.c | 21 | ||||
| -rw-r--r-- | c04/ex01/ft_putstr.c | 22 | ||||
| -rw-r--r-- | c04/ex02/ft_putnbr.c | 44 | ||||
| -rw-r--r-- | c04/ex03/ft_atoi.c | 50 | ||||
| -rw-r--r-- | c04/ex04/ft_putnbr_base.c | 70 | ||||
| -rw-r--r-- | c04/ex05/ft_atoi_base.c | 48 | ||||
| -rw-r--r-- | c04/main.c | 20 |
17 files changed, 548 insertions, 7 deletions
diff --git a/c02/ex06/ft_str_is_printable.c b/c02/ex06/ft_str_is_printable.c index edb8fe5..ad9e05a 100644 --- a/c02/ex06/ft_str_is_printable.c +++ b/c02/ex06/ft_str_is_printable.c @@ -6,7 +6,7 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/03 19:43:32 by cacharle #+# #+# */ -/* Updated: 2019/07/04 21:02:08 by cacharle ### ########.fr */ +/* Updated: 2019/07/05 10:05:14 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ int ft_str_is_printable(char *str) { while (*str != '\0') { - if (*str < ' ') + if (*str < ' ' || *str > '~') return (0); str++; } diff --git a/c02/ex08/ft_strlowcase.c b/c02/ex08/ft_strlowcase.c index 1c7b56c..b815630 100644 --- a/c02/ex08/ft_strlowcase.c +++ b/c02/ex08/ft_strlowcase.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_strlowercase.c :+: :+: :+: */ +/* ft_strlowcase.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2019/07/03 19:53:41 by cacharle #+# #+# */ -/* Updated: 2019/07/05 09:10:17 by cacharle ### ########.fr */ +/* Created: 2019/07/05 10:04:32 by cacharle #+# #+# */ +/* Updated: 2019/07/05 10:04:38 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/c02/ex10/ft_strlcpy.c b/c02/ex10/ft_strlcpy.c index aa6cfa5..ef4af68 100644 --- a/c02/ex10/ft_strlcpy.c +++ b/c02/ex10/ft_strlcpy.c @@ -6,7 +6,7 @@ /* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/04 15:27:01 by cacharle #+# #+# */ -/* Updated: 2019/07/05 09:44:10 by cacharle ### ########.fr */ +/* Updated: 2019/07/05 14:31:13 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,12 +27,14 @@ unsigned int ft_strlcpy(char *dest, char *src, unsigned int size) { unsigned int i; + if (size == 0) + return (ft_strlen(src)); i = 0; while (i < size - 1 && src[i] != '\0') { dest[i] = src[i]; i++; } - dest[size - 1] = '\0'; + dest[i] = '\0'; return (ft_strlen(src)); } 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]); +} diff --git a/c04/ex00/ft_strlen.c b/c04/ex00/ft_strlen.c new file mode 100644 index 0000000..271088d --- /dev/null +++ b/c04/ex00/ft_strlen.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/06 08:31:20 by cacharle #+# #+# */ +/* Updated: 2019/07/06 08:32:00 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_strlen(char *str) +{ + int counter; + + counter = 0; + while (str[counter]) + counter++; + return (counter); +} diff --git a/c04/ex01/ft_putstr.c b/c04/ex01/ft_putstr.c new file mode 100644 index 0000000..e98b5a0 --- /dev/null +++ b/c04/ex01/ft_putstr.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/06 07:22:38 by cacharle #+# #+# */ +/* Updated: 2019/07/06 08:34:03 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <unistd.h> + +void ft_putstr(char *str) +{ + while (*str) + { + write(1, str, 1); + str++; + } +} diff --git a/c04/ex02/ft_putnbr.c b/c04/ex02/ft_putnbr.c new file mode 100644 index 0000000..8a13dc5 --- /dev/null +++ b/c04/ex02/ft_putnbr.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/06 07:24:04 by cacharle #+# #+# */ +/* Updated: 2019/07/06 08:34:48 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <unistd.h> + +void ft_putchar(char c) +{ + write(1, &c, 1); +} + +void ft_putnbr(int nb) +{ + int i; + int rev_digits[100]; + unsigned int nbu; + + if (nb < 0) + { + ft_putchar('-'); + nbu = -nb; + } else + nbu = nb; + i = 0; + while (nbu > 0) + { + rev_digits[i] = nbu % 10; + nbu /= 10; + i++; + } + while (i > 0) + { + i--; + ft_putchar(rev_digits[i] + '0'); + } +} diff --git a/c04/ex03/ft_atoi.c b/c04/ex03/ft_atoi.c new file mode 100644 index 0000000..0d4247b --- /dev/null +++ b/c04/ex03/ft_atoi.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/06 07:31:38 by cacharle #+# #+# */ +/* Updated: 2019/07/06 08:36:47 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int pow10(int exponent) +{ + int accumulator; + + accumulator = 1; + while (exponent > 0) + { + accumulator *= 10; + exponent--; + } + return (accumulator); +} + +int ft_atoi(char *str) +{ + int is_negative; + int nb; + int i; + + while (*str == ' ' || *str == '\t' || *str == '\n' + || *str == '\v' || *str == '\f' || *str == '\r') + str++; + while (*str == '-' || *str == '+') + { + if (*str == '-') + is_negative = !is_negative; + str++; + } + i = 0; + while (str[i] >= '0' && str[i] <= '9') + { + nb += pow10(i) * (str[i] - '0'); + i++; + } + if (is_negative) + nb = -nb; + return (nb); +} diff --git a/c04/ex04/ft_putnbr_base.c b/c04/ex04/ft_putnbr_base.c new file mode 100644 index 0000000..6827475 --- /dev/null +++ b/c04/ex04/ft_putnbr_base.c @@ -0,0 +1,70 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_base.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/06 07:47:44 by cacharle #+# #+# */ +/* Updated: 2019/07/06 08:37:59 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int check_base(char *base) +{ + int i; + + if (!*base) + return (0); + while (*base) + { + if (*base == '-' || *base == '+') + return (0); + i = 0; + while (base[i]) + { + if (base[i] == *base) + return (0); + i++; + } + } + return (1); +} + +int my_strlen(char *str) +{ + int counter; + + counter = 0; + while (*str) + { + counter++; + str++; + } + return (counter); +} + +void ft_putnbr_base(int nbr, char *base) +{ + int radix; + int i; + int nb; + int rev_digits[1024]; + + if (!check_base(base)) + return; + radix = my_strlen(base); + nb = 0; + i = 0; + while (nb > 0) + { + rev_digits[i] = base[nb % radix]; + nb /= radix; + i++; + } + while (i > 0) + { + i--; + write(1, rev_digits + i, 1); + } +} diff --git a/c04/ex05/ft_atoi_base.c b/c04/ex05/ft_atoi_base.c new file mode 100644 index 0000000..b595a5c --- /dev/null +++ b/c04/ex05/ft_atoi_base.c @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi_base.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/06 08:07:17 by cacharle #+# #+# */ +/* Updated: 2019/07/06 08:30:16 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int my_pow(int base, int exponent) +{ + int accumulator; + + accumulator = 1; + while (exponent > 0) + { + accumulator *= base; + exponent--; + } + return (accumulator); +} + +int ft_atoi_base(char *str, char *base) +{ + int radix; + int i; + int j; + int nb; + + radix = 0; + while (base[radix]) + radix++; + i = 0; + while (str[i]) + i++; + j = 0; + while (i > 0) + { + nb += my_pow(radix, j); + nb %= radix; + i--; + j++; + } + return nb; +} diff --git a/c04/main.c b/c04/main.c new file mode 100644 index 0000000..a165b3c --- /dev/null +++ b/c04/main.c @@ -0,0 +1,20 @@ +#include <stdio.h> +#include <string.h> +#include "ex00/ft_strlen.c" +#include "ex01/ft_putstr.c" +#include "ex02/ft_putnbr.c" +#include "ex03/ft_atoi.c" +#include "ex04/ft_putnbr_base.c" +#include "ex05/ft_atoi_base.c" + +int main() +{ + char *s1 = "jesuis7"; + printf("%d\n", ft_strlen(s1)); + + char *s = "bonjour"; + ft_putstr(s); + printf("\n"); + + +} |
