├── swap_bits.c ├── is_power_of_two.c ├── max.c ├── alpha_mirror.c ├── search_and_replace.c ├── union.c ├── rotone.c ├── ft_list.h ├── list.h ├── wdmatch.c ├── inter.c ├── strstr.c ├── repeat_alpha.c ├── epur_str.c ├── ft_atoi_base.c ├── print_param.c ├── ft_list_foreach.c ├── strrev.c ├── first_word.c ├── print_memory.c ├── sort_int_tab.c ├── fprime.c ├── ft_itoa.c ├── ft_itoa_base.c ├── rev_wstr.c ├── sort_list.c ├── ft_list_remove_if.c ├── ft_split.c ├── rpn_calc.c ├── brackets.c ├── rostring.c └── check_mate.c /swap_bits.c: -------------------------------------------------------------------------------- 1 | unsigned char swap_bits(unsigned char octet) 2 | { 3 | return ((octet >> 4) + (octet << 4)); 4 | } 5 | -------------------------------------------------------------------------------- /is_power_of_two.c: -------------------------------------------------------------------------------- 1 | int isPowerOfTwo (unsigned int x) 2 | { 3 | while (((x % 2) == 0) && x > 1) /* While x is even and > 1 */ 4 | x /= 2; 5 | return (x == 1); 6 | } 7 | -------------------------------------------------------------------------------- /max.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | int max(int *tab, unsigned int len) 4 | { 5 | int i; 6 | int stock; 7 | 8 | i = 0; 9 | stock = 0; 10 | if (!(tab)) 11 | return(0); 12 | while (len) 13 | { 14 | if (tab[i] > stock) 15 | stock = tab[i]; 16 | i++; 17 | len--; 18 | } 19 | return(stock); 20 | } 21 | 22 | int main(void) 23 | { 24 | int tab[] = {42, 0, 5, 64, 2, 68, 1}; 25 | 26 | printf("max : %d", max(tab, 7)); 27 | return (0); 28 | } -------------------------------------------------------------------------------- /alpha_mirror.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void ft_putchar(char c) 4 | { 5 | write (1, &c, 1); 6 | } 7 | 8 | int main(int argc, char **argv) 9 | { 10 | const char tab[] = "zyxwvutsrqponmlkjihgfedcba"; 11 | char *str = argv[1]; 12 | int i; 13 | 14 | i = 0; 15 | if (argc == 2) 16 | { 17 | while (str[i]) 18 | { 19 | if (str[i] >= 'a' && str[i] <= 'z') 20 | str[i] = tab[str[i] - 'a']; 21 | if (str[i] >= 'A' && str[i] <= 'Z') 22 | str[i] = tab[str[i] - 'A'] - 32; 23 | ft_putchar(str[i]); 24 | i++; 25 | } 26 | } 27 | ft_putchar('\n'); 28 | return (0); 29 | } -------------------------------------------------------------------------------- /search_and_replace.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void ft_putchar(char c) 5 | { 6 | write(1, &c, 1); 7 | } 8 | 9 | int ft_strlen(char *s) 10 | { 11 | int i; 12 | 13 | i = 0; 14 | while (s[i] != '\0') 15 | i++; 16 | return (i); 17 | } 18 | 19 | void ft_putstr(char *s) 20 | { 21 | write (1, s, ft_strlen(s)); 22 | } 23 | 24 | int main(int argc, char **argv) 25 | { 26 | int i; 27 | 28 | i = 0; 29 | if (argc == 4 && ft_strlen(argv[2]) == 1 && ft_strlen(argv[3]) == 1) 30 | { 31 | while (argv[1][i]) 32 | { 33 | if (argv[1][i] == argv[2][0]) 34 | argv[1][i] = argv[3][0]; 35 | i++; 36 | } 37 | ft_putstr(argv[1]); 38 | } 39 | ft_putchar('\n'); 40 | return (0); 41 | } -------------------------------------------------------------------------------- /union.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void ft_putchar(char c) 4 | { 5 | write (1, &c, 1); 6 | } 7 | 8 | int main(int ac, char **av) 9 | { 10 | int tab[256]; 11 | unsigned char *s1; 12 | unsigned char *s2; 13 | int i; 14 | 15 | i = 0; 16 | if (ac == 3) 17 | { 18 | s1 = av[1]; 19 | s2 = av[2]; 20 | while (i < 256) 21 | { 22 | tab[i] = 0; 23 | i++; 24 | } 25 | i = 0; 26 | while (s1[i] != '\0') 27 | { 28 | if (tab[s1[i]] == 0) 29 | { 30 | ft_putchar(s1[i]); 31 | tab[s1[i]] = 1; 32 | } 33 | i++; 34 | } 35 | i = 0; 36 | while (s2[i] != '\0') 37 | { 38 | if (tab[(int)s2[i]] == 0) 39 | { 40 | ft_putchar(s2[i]); 41 | tab[s2[i]] = 1; 42 | } 43 | i++; 44 | } 45 | } 46 | ft_putchar('\n'); 47 | return (0); 48 | } 49 | -------------------------------------------------------------------------------- /rotone.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int ft_strlen(char *s) 4 | { 5 | int i; 6 | 7 | i = 0; 8 | while (s[i] != '\0') 9 | i++; 10 | return (i); 11 | } 12 | 13 | void ft_putstr(char *s) 14 | { 15 | write(1, s, ft_strlen(s)); 16 | } 17 | 18 | void ft_putchar(char c) 19 | { 20 | write(1, &c, 1); 21 | } 22 | 23 | int ft_isalpha(int c) 24 | { 25 | if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122)) 26 | return (1); 27 | return (0); 28 | } 29 | 30 | int main(int argc, char **argv) 31 | { 32 | int i; 33 | 34 | i = 0; 35 | if (argc == 2) 36 | { 37 | while (argv[1][i]) 38 | { 39 | if (ft_isalpha(argv[1][i])) 40 | { 41 | if (argv[1][i] == 'z' || argv[1][i] == 'Z') 42 | ft_putchar(argv[1][i] - 25); 43 | else 44 | ft_putchar(argv[1][i] + 1); 45 | } 46 | else 47 | ft_putchar(argv[1][i]); 48 | i++; 49 | } 50 | } 51 | ft_putchar('\n'); 52 | } -------------------------------------------------------------------------------- /ft_list.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_list.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: exam +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/10/24 11:19:36 by exam #+# #+# */ 9 | /* Updated: 2017/10/24 13:18:35 by exam ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | typedef struct s_list 14 | { 15 | struct s_list *next; 16 | void *data; 17 | } t_list; 18 | 19 | -------------------------------------------------------------------------------- /list.h: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* list.h :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: zaz +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2014/02/27 13:14:10 by zaz #+# #+# */ 9 | /* Updated: 2017/10/21 16:23:41 by jcharloi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | typedef struct s_list t_list; 14 | 15 | struct s_list 16 | { 17 | void *data; 18 | t_list *next; 19 | }; 20 | -------------------------------------------------------------------------------- /wdmatch.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int ft_strlen(char *str) 4 | { 5 | int i; 6 | 7 | i = 0; 8 | while (str[i] != '\0') 9 | i++; 10 | return (i); 11 | } 12 | 13 | void ft_putstr(char *str) 14 | { 15 | write(1, str, ft_strlen(str)); 16 | } 17 | 18 | int main(int ac, char **av) 19 | { 20 | char *av1; 21 | char *av2; 22 | int i; 23 | int j; 24 | int stock; 25 | 26 | i = 0; 27 | j = 0; 28 | stock = 0; 29 | av1 = av[1]; 30 | av2 = av[2]; 31 | if (ac == 3) 32 | { 33 | while (av1[i] != '\0') 34 | { 35 | while (av2[j] != '\0') 36 | { 37 | if (av1[i] == av2[j]) 38 | { 39 | stock++; 40 | j++; 41 | break ; 42 | } 43 | j++; 44 | } 45 | i++; 46 | } 47 | if (stock == ft_strlen(av1)) 48 | ft_putstr(av1); 49 | } 50 | ft_putstr("\n"); 51 | return (0); 52 | } 53 | -------------------------------------------------------------------------------- /inter.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void ft_putchar(char c) 4 | { 5 | write(1, &c, 1); 6 | } 7 | 8 | int ft_check(char *str, char c, int i) 9 | { 10 | int j; 11 | 12 | j = 0; 13 | while(j < i) 14 | { 15 | if (str[j] == c) 16 | return (0); 17 | ++j; 18 | } 19 | return (1); 20 | } 21 | 22 | void ft_iter(char *s1, char *s2) 23 | { 24 | int i; 25 | int j; 26 | 27 | i = 0; 28 | while (s1[i]) 29 | { 30 | if (ft_check(s1, s1[i], i) == 1) 31 | { 32 | j = 0; 33 | while (s2[j]) 34 | { 35 | if (s2[j] == s1[i]) 36 | { 37 | write(1, &s1[i], 1); 38 | break; 39 | } 40 | ++j; 41 | } 42 | } 43 | ++i; 44 | } 45 | } 46 | 47 | int main(int ac, char **av) 48 | { 49 | if (ac == 3) 50 | ft_iter(av[1], av[2]); 51 | write(1, "\n", 1); 52 | return (0); 53 | } 54 | 55 | // "1231" 56 | // //tab[10] = {0, 1, 1, 1, 0, 0, 0, 0, 0, 0}; 57 | // char tab[256]; 58 | // while (i < 256) 59 | // tab[i] = 0; 60 | 61 | // c = argv[1][i]; 62 | // if (tab[c] == 0) 63 | // { 64 | // ft_putchar(c); 65 | // tab[c] = 1; 66 | // } -------------------------------------------------------------------------------- /strstr.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void ft_putchar(char c) 4 | { 5 | write(1, &c, 1); 6 | } 7 | 8 | int ft_strlen(char *str) 9 | { 10 | int i = 0; 11 | 12 | while (str[i] != '\0') 13 | i++; 14 | return (i); 15 | } 16 | 17 | void ft_putstr(char *str) 18 | { 19 | write(1, str, ft_strlen(str)); 20 | } 21 | 22 | int compare(char *s1, char *s2) 23 | { 24 | int j = 0; 25 | int len = 0; 26 | 27 | while (s2[j] != '\0') 28 | { 29 | if (s2[j] == s1[j]) 30 | len++; 31 | j++; 32 | } 33 | if (len == ft_strlen(s2)) 34 | return (1); 35 | return (0); 36 | } 37 | 38 | int main(int argc, char **argv) 39 | { 40 | char *str1; 41 | char *str2; 42 | int i; 43 | 44 | i = 0; 45 | if (argc == 3) 46 | { 47 | str1 = argv[1]; 48 | str2 = argv[2]; 49 | while (str1[i] != '\0') 50 | { 51 | if (str1[i] == str2[0]) 52 | { 53 | if (compare(str1 + i, str2) == 1) 54 | { 55 | ft_putstr(str1 + i); 56 | ft_putchar('\n'); 57 | } 58 | return (0); 59 | } 60 | i++; 61 | } 62 | } 63 | ft_putchar('\n'); 64 | return (0); 65 | } 66 | -------------------------------------------------------------------------------- /repeat_alpha.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int ft_strlen(char *s) 4 | { 5 | int i; 6 | 7 | i = 0; 8 | while (s[i] != '\0') 9 | i++; 10 | return (i); 11 | } 12 | 13 | int ft_isalpha (int c) 14 | { 15 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) 16 | return (1); 17 | return (0); 18 | } 19 | 20 | void ft_putchar(char c) 21 | { 22 | write (1, &c, 1); 23 | } 24 | 25 | void ft_putstr(char *s) 26 | { 27 | write (1, s, ft_strlen(s)); 28 | } 29 | 30 | int main(int argc, char **argv) 31 | { 32 | int i; 33 | int stock; 34 | 35 | i = 0; 36 | stock = 0; 37 | if (argc == 2) 38 | { 39 | while (argv[1][i]) 40 | { 41 | if (ft_isalpha(argv[1][i])) 42 | { 43 | if (argv[1][i] >= 'a' && argv[1][i] <= 'z') 44 | stock = argv[1][i] - 96; 45 | if (argv[1][i] >= 'A' && argv[1][i] <= 'Z') 46 | stock = argv[1][i] - 64; 47 | while (stock) 48 | { 49 | ft_putchar(argv[1][i]); 50 | stock--; 51 | } 52 | } 53 | else 54 | ft_putchar(argv[1][i]); 55 | i++; 56 | } 57 | } 58 | ft_putchar('\n'); 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /epur_str.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void ft_putchar(char c) 4 | { 5 | write (1, &c, 1); 6 | } 7 | 8 | int ft_iswhitespace(int c) 9 | { 10 | if (c == '\t' || c == ' ') 11 | return (1); 12 | return (0); 13 | } 14 | 15 | int ft_isalpha(int c) 16 | { 17 | if (c >= 32 && c <= 126) 18 | return (1); 19 | return (0); 20 | } 21 | 22 | void epur_str(char *str) 23 | { 24 | int i; 25 | 26 | i = 0; 27 | while (ft_iswhitespace(str[i])) 28 | i++; 29 | while (str[i] != '\0') 30 | { 31 | if (ft_iswhitespace(str[i]) != 1) 32 | ft_putchar(str[i]); 33 | while (ft_iswhitespace(str[i]) && ft_iswhitespace(str[i + 1])) 34 | i++; 35 | if (ft_iswhitespace(str[i]) && ft_isalpha(str[i + 1])) 36 | ft_putchar(str[i]); 37 | i++; 38 | } 39 | } 40 | 41 | int main(int argc, char **argv) 42 | { 43 | char *str; 44 | 45 | str = argv[1]; 46 | if (argc == 2) 47 | epur_str(str); 48 | ft_putchar('\n'); 49 | return (0); 50 | } 51 | 52 | while (str[i] != '\0' && ((str[i] == ' ' || str[i] == '\t') && (str[i + 1] == ' ' || str[i + 1] == '\t'))) -------------------------------------------------------------------------------- /ft_atoi_base.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int ft_iswhitespace(char const c) 5 | { 6 | if (c == ' ' || c == '\n' || c == '\t' || c == '\v' 7 | || c == '\r' || c == '\f') 8 | return (1); 9 | return (0); 10 | } 11 | 12 | int base(int c, int base) 13 | { 14 | char *str = "0123456789abcdef"; 15 | char *str2 = "0123456789ABCDEF"; 16 | int i = 0; 17 | 18 | while (i < base) 19 | { 20 | if (c == str[i] || c == str2[i]) 21 | return (i); 22 | i++; 23 | } 24 | return (-1); 25 | } 26 | 27 | int ft_atoi_base(const char *str, int str_base) 28 | { 29 | int nb = 0; 30 | int negatif = 0; 31 | int i = 0; 32 | while (ft_iswhitespace(str[i])) 33 | i++; 34 | if (str[i] == '+' || str[i] == '-') 35 | { 36 | if (str[i] == '-') 37 | negatif = 1; 38 | i++; 39 | } 40 | while (base(str[i], str_base) != -1) 41 | { 42 | nb = nb * str_base; 43 | nb = nb + base(str[i], str_base); 44 | i++; 45 | } 46 | if (negatif) 47 | return (-nb); 48 | return (nb); 49 | } 50 | 51 | int main(int ac, char **av) 52 | { 53 | int nb; 54 | if (ac >= 3) 55 | { 56 | nb = ft_atoi_base(av[1], atoi(av[2])); 57 | printf("%d\n", nb); 58 | } 59 | return (0); 60 | } -------------------------------------------------------------------------------- /print_param.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void ft_putchar(char c) 4 | { 5 | write(1, &c, 1); 6 | } 7 | 8 | void ft_putstr(char *str) 9 | { 10 | int i; 11 | 12 | i = 0; 13 | while (str[i] != '\0') 14 | { 15 | ft_putchar(str[i]); 16 | i++; 17 | } 18 | } 19 | 20 | int ft_strcmp(char *s1, char *s2) 21 | { 22 | int i; 23 | 24 | i = 0; 25 | while (s1[i] == s2[i] && s1[i] != '\0' && s2[i] != '\0') 26 | { 27 | i++; 28 | if (s1[i] > s2[i] || s1[i] < s2[i]) 29 | return (s1[i] - s2[i]); 30 | } 31 | return (s1[i] - s2[i]); 32 | } 33 | 34 | void ft_argc(int argc, char **argv) 35 | { 36 | int i; 37 | 38 | i = 0; 39 | while (argc > 1) 40 | { 41 | ft_putstr(argv[i + 1]); 42 | ft_putchar('\n'); 43 | i++; 44 | argc--; 45 | } 46 | } 47 | 48 | int main(int argc, char **argv) 49 | { 50 | int i; 51 | int f; 52 | char *j; 53 | 54 | i = 1; 55 | j = 0; 56 | while (i < argc - 1) 57 | { 58 | f = 0; 59 | if (ft_strcmp(argv[i], argv[i + 1]) >= 0) 60 | { 61 | f = 1; 62 | j = argv[i]; 63 | argv[i] = argv[i + 1]; 64 | argv[i + 1] = j; 65 | } 66 | i++; 67 | if (f == 1) 68 | i = 1; 69 | } 70 | ft_argc(argc, argv); 71 | return (0); 72 | } 73 | -------------------------------------------------------------------------------- /ft_list_foreach.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_list_foreach.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jcharloi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/10/21 14:08:39 by jcharloi #+# #+# */ 9 | /* Updated: 2017/10/21 18:05:06 by jcharloi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "list.h" 14 | 15 | void ft_list_foreach(t_list *begin_list, void (*f)(void*)) 16 | { 17 | t_list *list_ptr; 18 | 19 | list_ptr = begin_list; 20 | while (list_ptr != NULL) 21 | { 22 | (*f)(list_ptr->data); 23 | list_ptr = list_ptr->next; 24 | } 25 | } 26 | 27 | -------------------------------------------------------------------------------- /strrev.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int ft_strlen(char *s) 7 | { 8 | int i; 9 | 10 | i = 0; 11 | while (s[i] != '\0') 12 | i++; 13 | return (i); 14 | } 15 | 16 | // char *ft_strrev(char *str) 17 | // { 18 | // char *strrev; 19 | // int i; 20 | // int j; 21 | 22 | // i = ft_strlen(str); 23 | // j = 0; 24 | // strrev = (char *)malloc(sizeof(char) * (i + 1)); 25 | // if (strrev == NULL) 26 | // return (NULL); 27 | // while (--i >= 0) 28 | // { 29 | // strrev[j] = str[i]; 30 | // //i--; 31 | // j++; 32 | // } 33 | // strrev[j] = '\0'; 34 | // return (strrev); 35 | // } 36 | 37 | // int main(int argc, char **argv) 38 | // { 39 | // char *str; 40 | 41 | // str = ft_strrev(argv[1]); 42 | // ft_putstr(str); 43 | // return (0); 44 | // } 45 | 46 | 47 | char *ft_strrev(char *str) 48 | { 49 | char tmp; 50 | int i; 51 | int j; 52 | 53 | i = 0; 54 | j = ft_strlen(str) - 1; 55 | while (i < j) 56 | { 57 | tmp = str[i]; 58 | str[i] = str[j]; 59 | str[j] = tmp; 60 | j--; 61 | i++; 62 | } 63 | return (str); 64 | } 65 | 66 | int main() 67 | { 68 | char *str = strdup("Quentin <3"); 69 | printf("%s\n", ft_strrev(str)); 70 | return (0); 71 | } 72 | -------------------------------------------------------------------------------- /first_word.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void ft_putchar(char c) 4 | { 5 | write (1, &c, 1); 6 | } 7 | 8 | int ft_strlen(char *s) 9 | { 10 | int i; 11 | 12 | i = 0; 13 | while (s[i] != '\0') 14 | i++; 15 | return (i); 16 | } 17 | 18 | int iswhitespace(int c) 19 | { 20 | if (c == ' ' || c == '\t') 21 | return (1); 22 | return (0); 23 | } 24 | 25 | void ft_putstr(char *s) 26 | { 27 | write (1, s, ft_strlen(s)); 28 | } 29 | 30 | // int main(int argc, char **argv) 31 | // { 32 | // int i; 33 | 34 | // i = 0; 35 | // if (argc == 2) 36 | // { 37 | // while(iswhitespace(argv[1][i])) 38 | // i++; 39 | // while(!(iswhitespace(argv[1][i])) && argv[1][i] != '\0') 40 | // { 41 | // ft_putchar(argv[1][i]); 42 | // i++; 43 | // } 44 | // } 45 | // ft_putchar('\n'); 46 | // return (0); 47 | // } 48 | 49 | 50 | int main(int argc, char **argv) 51 | { 52 | int i; 53 | 54 | if (argc == 2 && ft_strlen(argv[1]) > 0) 55 | { 56 | i = ft_strlen(argv[1]) - 1; 57 | while(iswhitespace(argv[1][i])) 58 | i--; 59 | while(!(iswhitespace(argv[1][i])) && i > 0) 60 | i--; 61 | if (iswhitespace(argv[1][i])) 62 | i++; 63 | while (!(iswhitespace(argv[1][i])) && argv[1][i] != '\0') 64 | { 65 | ft_putchar(argv[1][i]); 66 | i++; 67 | } 68 | } 69 | ft_putchar('\n'); 70 | return (0); 71 | } 72 | -------------------------------------------------------------------------------- /print_memory.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void ft_putchar(char c) 4 | { 5 | write(1, &c, 1); 6 | } 7 | 8 | void print_memory(const void *addr, size_t size) 9 | { 10 | const char *base = "0123456789abcdef"; 11 | size_t i = 0; 12 | unsigned char *str = (unsigned char*)addr; 13 | char line[17]; 14 | int nb; 15 | int j; 16 | 17 | //Tant que je suis inférieur à size OU que j'ai pas fini ma ligne 18 | while (i < size || i % 16 != 0) 19 | { 20 | //Si je suis inférieur à size 21 | if (i < size) 22 | { 23 | nb = str[i] / 16; 24 | ft_putchar(base[nb]); 25 | nb = str[i] % 16; 26 | ft_putchar(base[nb]); 27 | //Si c'est un caractere imprimable, je stocke, sinon '.' 28 | line[i % 16] = (str[i] >= 32 && str[i] <= 126) ? str[i] : '.'; 29 | } 30 | //Sinon je mets des espaces pour la derniere ligne 31 | else 32 | write(1, " ", 2); 33 | i++; 34 | if (i % 2 == 0) 35 | ft_putchar(' '); 36 | if (i % 16 == 0) 37 | { 38 | j = 0; 39 | while (j < 16) 40 | { 41 | //Si je ne suis pas a la fin du tableau 42 | //(i - 16 == debut de la ligne) + j pour savoir ou j'en suis 43 | //dans la derniere ligne 44 | if (i - 16 + j >= size) 45 | break ; 46 | ft_putchar(line[j++]); 47 | } 48 | ft_putchar('\n'); 49 | } 50 | } 51 | } 52 | 53 | int main(void) 54 | { 55 | char tab[] = {48,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}; 56 | print_memory(tab, sizeof(tab)); 57 | return 0; 58 | } -------------------------------------------------------------------------------- /sort_int_tab.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* sort_int_tab.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jcharloi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/10/21 18:12:14 by jcharloi #+# #+# */ 9 | /* Updated: 2017/10/21 19:04:18 by jcharloi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | int sort_tab(int *tab, unsigned int size) 14 | { 15 | unsigned int i; 16 | 17 | i = 0; 18 | while (i < size - 1) 19 | { 20 | if (tab[i] > tab[i + 1]) 21 | return (1); 22 | i++; 23 | } 24 | return (0); 25 | } 26 | 27 | int *sort_int_tab(int *tab, unsigned int size) 28 | { 29 | unsigned int i; 30 | int swap; 31 | 32 | swap = 0; 33 | while (sort_tab(tab, size)) 34 | { 35 | i = 0; 36 | while (i < size - 1) 37 | { 38 | if (tab[i] > tab[i + 1]) 39 | { 40 | swap = tab[i]; 41 | tab[i] = tab[i + 1]; 42 | tab[i + 1] = swap; 43 | } 44 | i++; 45 | } 46 | } 47 | return (tab); 48 | } 49 | -------------------------------------------------------------------------------- /fprime.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* fprime.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jcharloi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/10/19 17:20:46 by jcharloi #+# #+# */ 9 | /* Updated: 2017/12/11 17:12:41 by jcharloi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | #include 15 | 16 | int main(int ac, char **av) 17 | { 18 | int div = 2; 19 | int nb; 20 | 21 | if (ac == 2) 22 | { 23 | nb = atoi(av[1]); 24 | if (nb <= 1) 25 | { 26 | printf("%d\n", nb); 27 | return (0); 28 | } 29 | while (nb != 1) 30 | { 31 | if (nb % div == 0) 32 | { 33 | printf("%d\n", div); 34 | nb = nb / div; 35 | div = 2; 36 | } 37 | else 38 | div++; 39 | } 40 | } 41 | printf("\n"); 42 | return (0); 43 | } 44 | -------------------------------------------------------------------------------- /ft_itoa.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_itoa.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jcharloi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/10/17 17:04:12 by jcharloi #+# #+# */ 9 | /* Updated: 2017/12/11 18:57:03 by jcharloi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | #include 15 | 16 | int len(long nb) 17 | { 18 | int len; 19 | 20 | len = 0; 21 | if (nb < 0) 22 | { 23 | nb = nb * -1; 24 | len++; 25 | } 26 | while (nb > 0) 27 | { 28 | nb = nb / 10; 29 | len++; 30 | } 31 | return (len); 32 | } 33 | 34 | char *ft_itoa(int nb) 35 | { 36 | char *str; 37 | long n; 38 | int i; 39 | 40 | n = nb; 41 | i = len(n); 42 | if (!(str = (char*)malloc(sizeof(char) * (i + 1)))) 43 | return (NULL); 44 | str[i--] = '\0'; 45 | if (n == 0) 46 | { 47 | str[0] = 48; 48 | return (str); 49 | } 50 | if (n < 0) 51 | { 52 | str[0] = '-'; 53 | n = n * -1; 54 | } 55 | while (n > 0) 56 | { 57 | str[i] = 48 + (n % 10); 58 | n = n / 10; 59 | i--; 60 | } 61 | return (str); 62 | } 63 | 64 | int main(void) 65 | { 66 | printf("%s\n", ft_itoa(123156)); 67 | return (0); 68 | } 69 | -------------------------------------------------------------------------------- /ft_itoa_base.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_itoa_base.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jcharloi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/12/12 10:45:50 by exam #+# #+# */ 9 | /* Updated: 2018/01/12 17:41:43 by jcharloi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | #include 15 | #include 16 | 17 | int size(long nb, int base) 18 | { 19 | int len = 0; 20 | 21 | if (nb < 0 && base == 10) 22 | { 23 | len++; 24 | nb = nb * -1; 25 | } 26 | else if (nb < 0) 27 | nb = nb * -1; 28 | while (nb > 0) 29 | { 30 | len++; 31 | nb = nb / base; 32 | } 33 | return (len); 34 | } 35 | 36 | char *ft_itoa_base(int value, int base) 37 | { 38 | char *str; 39 | char tab[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; 40 | int i; 41 | int o; 42 | long nb; 43 | 44 | nb = value; 45 | i = size(nb, base); 46 | str = (char*)malloc(sizeof(char) * (i + 1)); 47 | str[i--] = '\0'; 48 | if (nb == 0) 49 | { 50 | str[0] = '0'; 51 | return (str); 52 | } 53 | if (nb < 0 && base == 10) 54 | { 55 | str[0] = '-'; 56 | nb = nb * -1; 57 | } 58 | else if (nb < 0) 59 | nb = nb * -1; 60 | while (nb > 0) 61 | { 62 | o = nb % base; 63 | str[i] = tab[o]; 64 | nb = nb / base; 65 | i--; 66 | } 67 | return (str); 68 | } -------------------------------------------------------------------------------- /rev_wstr.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* rev_wstr.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jcharloi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/10/22 15:16:31 by jcharloi #+# #+# */ 9 | /* Updated: 2017/10/22 20:11:30 by jcharloi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | int ft_strlen(char *str) 19 | { 20 | int i = 0; 21 | 22 | while (str[i] != '\0') 23 | i++; 24 | return (i); 25 | } 26 | 27 | void ft_putstr(char *str) 28 | { 29 | write(1, str, ft_strlen(str)); 30 | } 31 | 32 | void fonction(char *str) 33 | { 34 | char *copy; 35 | int size; 36 | int i; 37 | int o; 38 | int mot; 39 | 40 | size = 1; 41 | o = 0; 42 | mot = 0; 43 | if (!(copy = (char *)malloc(sizeof(char) * ft_strlen(str) + 1))) 44 | return ; 45 | i = ft_strlen(str) - 1; 46 | while (i >= 0) 47 | { 48 | while (str[i] != ' ' && i >= 0) 49 | { 50 | size++; 51 | i--; 52 | } 53 | size++; 54 | i++; 55 | while (str[i] != '\0' && str[i] != ' ') 56 | { 57 | copy[o] = str[i]; 58 | o++; 59 | i++; 60 | } 61 | if (size <= ft_strlen(str) + 1) 62 | copy[o++] = ' '; 63 | mot++; 64 | i = ft_strlen(str) - size; 65 | } 66 | //copy[i] = '\0'; 67 | ft_putstr(copy); 68 | //noublie pas le \0 69 | } 70 | 71 | int main(int argc, char **argv) 72 | { 73 | char *str; 74 | 75 | if (argc == 2) 76 | { 77 | str = argv[1]; 78 | fonction(str); 79 | } 80 | ft_putstr("\n"); 81 | return (0); 82 | } 83 | -------------------------------------------------------------------------------- /sort_list.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* sort_list.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jcharloi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/10/11 14:17:43 by jcharloi #+# #+# */ 9 | /* Updated: 2017/10/17 16:53:00 by jcharloi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include "list.h" 14 | #include 15 | #include 16 | 17 | int croissant(int a, int b) 18 | { 19 | if (a <= b) //1 < 2 = 1 20 | return (1); 21 | else // 2 > 1 = 0 22 | return (0); 23 | } 24 | 25 | int tri(t_list *lst, int (*cmp)(int, int)) 26 | { 27 | while (lst->next != NULL) 28 | { 29 | if ((*cmp)(lst->data, lst->next->data) == 0) 30 | return (0); 31 | lst = lst->next; 32 | } 33 | return (1); 34 | } 35 | 36 | t_list *sort_list(t_list *lst, int (*cmp)(int, int)) 37 | { 38 | t_list *first; 39 | int swap; 40 | 41 | swap = 0; 42 | first = lst; 43 | while (tri(first, croissant) == 0) 44 | { 45 | lst = first; 46 | while (lst->next != NULL) 47 | { 48 | if ((*cmp)(lst->data, lst->next->data) == 0) 49 | { 50 | swap = lst->data; 51 | lst->data = lst->next->data; 52 | lst->next->data = swap; 53 | } 54 | lst = lst->next; 55 | } 56 | } 57 | return (first); 58 | } 59 | 60 | int main(void) 61 | { 62 | t_list *lst; 63 | 64 | lst = (t_list*)malloc(sizeof(t_list)); 65 | lst->data = 20; 66 | lst->next = (t_list*)malloc(sizeof(t_list)); 67 | lst->next->data = 10; 68 | lst->next->next = (t_list*)malloc(sizeof(t_list)); 69 | lst->next->next->data = 0; 70 | lst->next->next->next = NULL; 71 | 72 | lst = sort_list(lst, croissant); 73 | 74 | while (lst != NULL) 75 | { 76 | printf("%d\n", lst->data); 77 | lst = lst->next; 78 | } 79 | 80 | return (0); 81 | } 82 | 83 | -------------------------------------------------------------------------------- /ft_list_remove_if.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_list_remove_if.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jcharloi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/10/24 11:12:02 by exam #+# #+# */ 9 | /* Updated: 2017/12/11 14:28:04 by jcharloi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | #include "ft_list.h" 15 | #include 16 | 17 | int cmp(void *data, void *data_ref) 18 | { 19 | int *i = data; 20 | int *j = data_ref; 21 | 22 | if (*i == *j) 23 | return (0); 24 | return (1); 25 | } 26 | 27 | void ft_list_remove_if(t_list **begin_list, void *data_ref, int (*cmp)()) 28 | { 29 | t_list *lst; 30 | t_list *tmp; 31 | int i = 0; 32 | 33 | if (begin_list == NULL || *begin_list == NULL) 34 | return ; 35 | tmp = *begin_list; 36 | while ((*cmp)((*begin_list)->data, data_ref) == 0) 37 | { 38 | tmp = (*begin_list)->next; 39 | free(*begin_list); 40 | (*begin_list) = tmp; 41 | } 42 | lst = *begin_list; 43 | tmp = *begin_list; 44 | while (lst != NULL) 45 | { 46 | if ((*cmp)(lst->data, data_ref) == 0) 47 | { 48 | tmp->next = lst->next; 49 | free(lst); 50 | lst = tmp; 51 | i = 0; 52 | } 53 | if (i > 0) 54 | tmp = tmp->next; 55 | if (lst != NULL) 56 | lst = lst->next; 57 | i++; 58 | } 59 | } 60 | 61 | int main(void) 62 | { 63 | t_list *lst; 64 | int i = 1; 65 | int j = 2; 66 | 67 | lst = (t_list*)malloc(sizeof(t_list)); 68 | lst->data = &j; 69 | lst->next = (t_list*)malloc(sizeof(t_list)); 70 | lst->next->data = &j; 71 | lst->next->next = (t_list*)malloc(sizeof(t_list)); 72 | lst->next->next->data = &i; 73 | lst->next->next->next = (t_list*)malloc(sizeof(t_list)); 74 | lst->next->next->next->data = &i; 75 | lst->next->next->next->next = NULL; 76 | 77 | ft_list_remove_if(&lst, &j, cmp); 78 | 79 | while (lst != NULL) 80 | { 81 | printf("%d\n", *(int*)lst->data); 82 | lst = lst->next; 83 | } 84 | 85 | return (0); 86 | } 87 | 88 | -------------------------------------------------------------------------------- /ft_split.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* ft_split.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jcharloi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/11/22 17:04:14 by jcharloi #+# #+# */ 9 | /* Updated: 2017/11/22 19:27:28 by jcharloi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | #include 15 | 16 | int nbr_words(char *str) 17 | { 18 | int nb; 19 | int i; 20 | 21 | nb = 0; 22 | i = 0; 23 | while (str[i] == '\t' || str[i] == ' ' || str[i] == '\n') 24 | i++; 25 | while (str[i] != '\0') 26 | { 27 | while (str[i] != '\0' && str[i] != '\t' && str[i] != ' ' && str[i] != '\n') 28 | i++; 29 | if (str[i] == '\0') 30 | { 31 | nb++; 32 | break ; 33 | } 34 | if (str[i] == '\t' || str[i] == ' ' || str[i] == '\n') 35 | nb++; 36 | while (str[i] == '\t' || str[i] == ' ' || str[i] == '\n') 37 | i++; 38 | } 39 | return (nb); 40 | } 41 | 42 | char **ft_split(char *str) 43 | { 44 | char **tab; 45 | int words; 46 | int letters; 47 | int var; 48 | int i; 49 | int o; 50 | 51 | i = 0; 52 | o = 0; 53 | if (str == NULL) 54 | { 55 | tab = (char**)malloc(sizeof(char*)); 56 | tab[o] = NULL; 57 | return (tab); 58 | } 59 | words = nbr_words(str); 60 | if (!(tab = (char**)malloc(sizeof(char*) * words + 1))) 61 | return (NULL); 62 | while (str[i] == '\t' || str[i] == ' ' || str[i] == '\n') 63 | i++; 64 | while (str[i] != '\0' && o < words) 65 | { 66 | letters = 0; 67 | while (str[i] != '\0' && str[i] != '\t' && str[i] != ' ' && str[i] != '\n') 68 | { 69 | letters++; 70 | i++; 71 | } 72 | if (!(tab[o] = (char*)malloc(sizeof(char) * letters))) 73 | return (NULL); 74 | while (str[i] == '\t' || str[i] == ' ' || str[i] == '\n') 75 | i++; 76 | o++; 77 | } 78 | tab[o] = NULL; 79 | i = 0; 80 | o = 0; 81 | var = 0; 82 | while (str[i] == '\t' || str[i] == ' ' || str[i] == '\n') 83 | i++; 84 | while (str[i] != '\0') 85 | { 86 | var = 0; 87 | while (str[i] != '\0' && str[i] != '\t' && str[i] != ' ' && str[i] != '\n') 88 | { 89 | tab[o][var] = str[i]; 90 | var++; 91 | i++; 92 | } 93 | if (str[i] == '\t' || str[i] == ' ' || str[i] == '\n') 94 | o++; 95 | while (str[i] == '\t' || str[i] == ' ' || str[i] == '\n') 96 | i++; 97 | } 98 | return (tab); 99 | } 100 | 101 | -------------------------------------------------------------------------------- /rpn_calc.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* rpn_calc.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jcharloi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/01/15 15:45:36 by jcharloi #+# #+# */ 9 | /* Updated: 2018/01/20 19:12:50 by jcharloi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | #include 15 | #include 16 | 17 | int ft_strlen(char *str) 18 | { 19 | int i; 20 | 21 | i = 0; 22 | while (str[i] != '\0') 23 | i++; 24 | return (i); 25 | } 26 | 27 | int ft_isdigit(char c) 28 | { 29 | if (c >= '0' && c <= '9') 30 | return (1); 31 | return (0); 32 | } 33 | 34 | int is_operateur(char *str) 35 | { 36 | int i; 37 | 38 | i = 0; 39 | if (str[i] == '*' || str[i] == '+' || str[i] == '-' || str[i] == '%' || str[i] == '/') 40 | { 41 | if (ft_isdigit(str[i + 1]) == 0) 42 | return (1); 43 | } 44 | return (0); 45 | } 46 | 47 | long *rpn_calc(char *str) 48 | { 49 | long *tab; 50 | int i; 51 | int j; 52 | 53 | i = 0; 54 | j = 0; 55 | if (!(tab = (long*)malloc(sizeof(long) * ft_strlen(str)))) 56 | return (NULL); 57 | while (str[i] != '\0') 58 | { 59 | while (is_operateur(str + i) == 0) 60 | { 61 | tab[j] = atoi(str + i); 62 | j++; 63 | while (str[i] != '\0' && str[i] != ' ') 64 | i++; 65 | if (str[i] == '\0') 66 | { 67 | printf("Error\n"); 68 | return (NULL); 69 | } 70 | while (str[i] == ' ') 71 | i++; 72 | } 73 | if (j < 2) 74 | { 75 | printf("Error\n"); 76 | return (NULL); 77 | } 78 | if (str[i] == '/') 79 | { 80 | if (tab[j - 1] == 0) 81 | { 82 | printf("Error\n"); 83 | return (NULL); 84 | } 85 | tab[j - 2] = tab[j - 2] / tab[j - 1]; 86 | } 87 | else if (str[i] == '-') 88 | tab[j - 2] = tab[j - 2] - tab[j - 1]; 89 | else if (str[i] == '+') 90 | tab[j - 2] = tab[j - 2] + tab[j - 1]; 91 | else if (str[i] == '*') 92 | tab[j - 2] = tab[j - 2] * tab[j - 1]; 93 | else if (str[i] == '%') 94 | { 95 | if (tab[j - 1] == 0) 96 | { 97 | printf("Error\n"); 98 | return (NULL); 99 | } 100 | tab[j - 2] = tab[j - 2] % tab[j - 1]; 101 | } 102 | j--; 103 | i++; 104 | while (str[i] == ' ') 105 | i++; 106 | } 107 | if (j > 1) 108 | { 109 | printf("Error\n"); 110 | return (NULL); 111 | } 112 | return (tab); 113 | } 114 | 115 | int main(int argc, char **argv) 116 | { 117 | long *tab; 118 | 119 | if (argc == 2 && argv[1][0] != '\0') 120 | { 121 | tab = rpn_calc(argv[1]); 122 | if (tab != NULL) 123 | printf("%ld\n", tab[0]); 124 | return (0); 125 | } 126 | printf("Error\n"); 127 | return (0); 128 | } 129 | -------------------------------------------------------------------------------- /brackets.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* brackets.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jcharloi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2018/01/14 14:59:48 by jcharloi #+# #+# */ 9 | /* Updated: 2018/01/14 19:02:48 by jcharloi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | #include 15 | 16 | int ft_strlen(char *str) 17 | { 18 | int i = 0; 19 | 20 | while (str[i] != '\0') 21 | i++; 22 | return (i); 23 | } 24 | 25 | void ft_putstr(char *str) 26 | { 27 | write(1, str, ft_strlen(str)); 28 | } 29 | 30 | int is_symbole(char c) 31 | { 32 | if (c == '(' || c == '[' || c == '{') 33 | return (1); 34 | return (0); 35 | } 36 | 37 | int is_rsymbole(char c) 38 | { 39 | if (c == ')' || c == ']' || c == '}') 40 | return (1); 41 | return (0); 42 | } 43 | 44 | int parcours(char *str) 45 | { 46 | int i; 47 | 48 | i = 0; 49 | while (str[i] != '\0') 50 | { 51 | if (is_rsymbole(str[i]) == 1) 52 | { 53 | return (1); 54 | } 55 | i++; 56 | } 57 | return (0); 58 | } 59 | 60 | void brackets(char *str) 61 | { 62 | int i; 63 | int endroit; 64 | char c; 65 | int test; 66 | 67 | i = ft_strlen(str); 68 | while (i >= 0) 69 | { 70 | test = 0; 71 | endroit = 0; 72 | c = '\0'; 73 | i = ft_strlen(str); 74 | while (i >= 0) 75 | { 76 | if (is_symbole(str[i]) == 1) 77 | { 78 | c = str[i]; 79 | endroit = i; 80 | break ; 81 | } 82 | i--; 83 | } 84 | if (c == '\0' && parcours(str) == 0) 85 | { 86 | ft_putstr("OK"); 87 | return ; 88 | } 89 | else if (c == '\0' && parcours(str) == 1) 90 | { 91 | ft_putstr("Error"); 92 | return ; 93 | } 94 | while (str[i] != '\0') 95 | { 96 | if (is_rsymbole(str[i]) == 1) 97 | { 98 | test = 1; 99 | if (c == '(') 100 | { 101 | if (str[i] != ')') 102 | { 103 | ft_putstr("Error"); 104 | return ; 105 | } 106 | else 107 | { 108 | str[i] = ' '; 109 | str[endroit] = ' '; 110 | break ; 111 | } 112 | } 113 | else if (c == '[') 114 | { 115 | if (str[i] != ']') 116 | { 117 | ft_putstr("Error"); 118 | return ; 119 | } 120 | else 121 | { 122 | str[i] = ' '; 123 | str[endroit] = ' '; 124 | break ; 125 | } 126 | } 127 | else if (c == '{') 128 | { 129 | if (str[i] != '}') 130 | { 131 | ft_putstr("Error"); 132 | return ; 133 | } 134 | else 135 | { 136 | str[i] = ' '; 137 | str[endroit] = ' '; 138 | break ; 139 | } 140 | } 141 | } 142 | i++; 143 | } 144 | if (test == 0) 145 | { 146 | ft_putstr("Error"); 147 | return ; 148 | } 149 | } 150 | ft_putstr("OK"); 151 | } 152 | 153 | int main(int argc, char **argv) 154 | { 155 | int count; 156 | 157 | count = 1; 158 | if (argc > 1) 159 | { 160 | while (count < argc) 161 | { 162 | brackets(argv[count]); 163 | count++; 164 | if (count < argc) 165 | ft_putstr("\n"); 166 | } 167 | } 168 | ft_putstr("\n"); 169 | return (0); 170 | } -------------------------------------------------------------------------------- /rostring.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* rostring.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jcharloi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/10/19 13:40:03 by jcharloi #+# #+# */ 9 | /* Updated: 2017/10/19 17:08:57 by jcharloi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | #include 15 | #include 16 | 17 | void ft_putchar(char c) 18 | { 19 | write(1, &c, 1); 20 | } 21 | 22 | int ft_strlen(char *str) 23 | { 24 | int i; 25 | 26 | i = 0; 27 | while (str[i] != '\0') 28 | i++; 29 | return (i); 30 | } 31 | 32 | void ft_putstr(char *str) 33 | { 34 | write(1, str, ft_strlen(str)); 35 | } 36 | 37 | char *ft_strcpy(char *str) 38 | { 39 | char *copy; 40 | int i; 41 | int o; 42 | 43 | i = 0; 44 | o = 0; 45 | if (!(copy = (char*)malloc(sizeof(char) * ft_strlen(str)))) 46 | return (NULL); 47 | while (str[i] != '\0' && (str[i] == ' ' || str[i] == '\t')) 48 | i++; 49 | while (str[i] != '\0' && str[i] != ' ' && str[i] != '\t') 50 | { 51 | copy[o] = str[i]; 52 | i++; 53 | o++; 54 | } 55 | copy[o] = '\0'; 56 | return (copy); 57 | } 58 | 59 | void rostring(char *str) 60 | { 61 | char *copy; 62 | int i; 63 | int neg; 64 | 65 | i = 0; 66 | neg = 0; 67 | copy = ft_strcpy(str); 68 | if (copy == NULL) 69 | return ; 70 | while (str[i] != '\0' && (str[i] == ' ' || str[i] == '\t')) 71 | i++; 72 | while (str[i] != '\0' && str[i] != ' ' && str[i] != '\t') 73 | i++; 74 | while (str[i] != '\0' && (str[i] == ' ' || str[i] == '\t')) 75 | i++; 76 | while (str[i] != '\0') 77 | { 78 | while (str[i] != '\0' && str[i] != ' ' && str[i] != '\t') 79 | { 80 | ft_putchar(str[i]); 81 | neg = 1; 82 | i++; 83 | } 84 | while (str[i] != '\0' && (str[i] == ' ' || str[i] == '\t')) 85 | i++; 86 | if (neg == 1) 87 | ft_putchar(' '); 88 | } 89 | ft_putstr(copy); 90 | free(copy); 91 | } 92 | 93 | int main(int argc, char **argv) 94 | { 95 | char *str; 96 | 97 | if (argc > 1) 98 | { 99 | str = argv[1]; 100 | rostring(str); 101 | } 102 | ft_putchar('\n'); 103 | return (0); 104 | } 105 | 106 | -------------------------------------------------------------- 107 | 108 | #include 109 | #include 110 | #include 111 | 112 | int ft_strlen(char *str) 113 | { 114 | int i = 0; 115 | 116 | while (str[i] != '\0') 117 | i++; 118 | return (i); 119 | } 120 | 121 | void ft_putstr(char *str) 122 | { 123 | write(1, str, ft_strlen(str)); 124 | } 125 | 126 | void ft_putchar(char c) 127 | { 128 | write(1, &c, 1); 129 | } 130 | 131 | int len_first_word(char *str) 132 | { 133 | int word = 0; 134 | int i = 0; 135 | 136 | while (str[i] == ' ' || str[i] == '\t') 137 | i++; 138 | while (str[i] != '\0' && str[i] != ' ' && str[i] != '\t') 139 | { 140 | word++; 141 | i++; 142 | } 143 | return (word); 144 | } 145 | 146 | char *get_first_word(char *str) 147 | { 148 | char *word; 149 | int first; 150 | int i; 151 | int j; 152 | 153 | i = 0; 154 | j = 0; 155 | first = len_first_word(str); 156 | if (!(word = (char*)malloc(sizeof(char) * (first + 1)))) 157 | return (NULL); 158 | while (str[i] == ' ' || str[i] == '\t') 159 | i++; 160 | while (str[i] != '\0' && str[i] != ' ' && str[i] != '\t') 161 | { 162 | word[j] = str[i]; 163 | j++; 164 | i++; 165 | } 166 | word[j] = '\0'; 167 | return (word); 168 | } 169 | 170 | char *fonction(char *str) 171 | { 172 | char *first_word; 173 | char *copy; 174 | int i = 0; 175 | int o = 0; 176 | 177 | first_word = get_first_word(str); 178 | if (first_word == NULL) 179 | return (NULL); 180 | if (!(copy = (char*)malloc(sizeof(char) * (ft_strlen(str) + 1)))) 181 | return (NULL); 182 | while (str[i] == ' ' || str[i] == '\t') 183 | i++; 184 | while (str[i] != '\0' && str[i] != ' ' && str[i] != '\t') 185 | i++; 186 | while (str[i] == ' ' || str[i] == '\t') 187 | i++; 188 | while (str[i] != '\0') 189 | { 190 | if (o > 0) 191 | copy[o++] = ' '; 192 | while (str[i] != '\0' && str[i] != ' ' && str[i] != '\t') 193 | { 194 | copy[o] = str[i]; 195 | o++; 196 | i++; 197 | } 198 | while (str[i] == ' ' || str[i] == '\t') 199 | i++; 200 | if (str[i] == '\0') 201 | copy[o++] = ' '; 202 | } 203 | copy[o] = '\0'; 204 | ft_putstr(copy); 205 | free(copy); 206 | return (first_word); 207 | } 208 | 209 | int main(int argc, char **argv) 210 | { 211 | char *str; 212 | char *first_word; 213 | 214 | if (argc > 1) 215 | { 216 | str = argv[1]; 217 | first_word = fonction(str); 218 | if (first_word == NULL) 219 | return (0); 220 | ft_putstr(first_word); 221 | free(first_word); 222 | } 223 | ft_putstr("\n"); 224 | return (0); 225 | } 226 | -------------------------------------------------------------------------------- /check_mate.c: -------------------------------------------------------------------------------- 1 | /* ************************************************************************** */ 2 | /* */ 3 | /* ::: :::::::: */ 4 | /* check_mate.c :+: :+: :+: */ 5 | /* +:+ +:+ +:+ */ 6 | /* By: jcharloi +#+ +:+ +#+ */ 7 | /* +#+#+#+#+#+ +#+ */ 8 | /* Created: 2017/12/06 16:12:20 by jcharloi #+# #+# */ 9 | /* Updated: 2017/12/07 13:52:43 by jcharloi ### ########.fr */ 10 | /* */ 11 | /* ************************************************************************** */ 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | int ft_strlen(char *str) 19 | { 20 | int i = 0; 21 | while (str[i] != '\0') 22 | i++; 23 | return (i); 24 | } 25 | 26 | void ft_putstr(char *str) 27 | { 28 | write(1, str, ft_strlen(str)); 29 | } 30 | 31 | char *ft_strcpy(char *dst, char *src) 32 | { 33 | int i; 34 | 35 | i = 0; 36 | while (src[i] != '\0') 37 | { 38 | dst[i] = src[i]; 39 | i++; 40 | } 41 | dst[i] = '\0'; 42 | return (dst); 43 | } 44 | 45 | char **create_tab(int len, char **argv) 46 | { 47 | char **tab; 48 | int i; 49 | 50 | i = 1; 51 | if (!(tab = (char**)malloc(sizeof(char*) * len + 1))) 52 | return (NULL); 53 | tab[len] = NULL; 54 | while (i < len + 1) 55 | { 56 | if (!(tab[i - 1] = (char*)malloc(sizeof(char) * len + 1))) 57 | return (NULL); 58 | tab[i - 1][len] = '\0'; 59 | tab[i - 1] = ft_strcpy(tab[i - 1], argv[i]); 60 | i++; 61 | } 62 | return (tab); 63 | } 64 | 65 | int check_rook(char **tab, int y, int x) 66 | { 67 | int i; 68 | int j; 69 | 70 | //printf("hjhr\n"); 71 | j = x + 1; 72 | //a droite 73 | while (tab[y][j] != '\0') 74 | { 75 | if (tab[y][j] == 'P' || tab[y][j] == 'B' || tab[y][j] == 'Q' || tab[y][j] == 'R') 76 | return (0); 77 | if (tab[y][j] == 'K') 78 | { 79 | ft_putstr("Success\n"); 80 | return (1); 81 | } 82 | j++; 83 | } 84 | 85 | j = x - 1; 86 | //printf("j : %d\n", j); 87 | //a gauche 88 | while (j >= 0) 89 | { 90 | if (tab[y][j] == 'P' || tab[y][j] == 'B' || tab[y][j] == 'Q' || tab[y][j] == 'R') 91 | return (0); 92 | if (tab[y][j] == 'K') 93 | { 94 | ft_putstr("Success\n"); 95 | return (1); 96 | } 97 | j--; 98 | } 99 | //printf("b\n"); 100 | i = y + 1; 101 | //en bas 102 | while (tab[i] != NULL) 103 | { 104 | if (tab[i][x] == 'P' || tab[i][x] == 'B' || tab[i][x] == 'Q' || tab[i][x] == 'R') 105 | return (0); 106 | if (tab[i][x] == 'K') 107 | { 108 | ft_putstr("Success\n"); 109 | return (1); 110 | } 111 | i++; 112 | } 113 | i = y - 1; 114 | //en haut 115 | //printf("i : %d\n", i); 116 | while (i >= 0) 117 | { 118 | if (tab[i][x] == 'P' || tab[i][x] == 'B' || tab[i][x] == 'Q' || tab[i][x] == 'R') 119 | return (0); 120 | if (tab[i][x] == 'K') 121 | { 122 | ft_putstr("Success\n"); 123 | return (1); 124 | } 125 | i--; 126 | } 127 | return (0); 128 | } 129 | 130 | int check_pawn(char **tab, int y, int x) 131 | { 132 | if (y > 0 && (tab[y - 1][x - 1] == 'K' || tab[y - 1][x + 1] == 'K')) 133 | { 134 | ft_putstr("Success\n"); 135 | return (1); 136 | } 137 | return (0); 138 | } 139 | 140 | int check_bishop(char **tab, int y, int x) 141 | { 142 | int i; 143 | int j; 144 | 145 | i = y + 1; 146 | j = x + 1; 147 | //en bas a droite; 148 | while (tab[i] != NULL) 149 | { 150 | if (tab[i][j] == 'P' || tab[i][j] == 'B' || tab[i][j] == 'Q' || tab[i][j] == 'R') 151 | return (0); 152 | if (tab[i][j] == 'K') 153 | { 154 | ft_putstr("Success\n"); 155 | return (1); 156 | } 157 | i++; 158 | j++; 159 | } 160 | i = y + 1; 161 | j = x - 1; 162 | //en bas a gauche 163 | while (tab[i] != NULL) 164 | { 165 | if (tab[i][j] == 'P' || tab[i][j] == 'B' || tab[i][j] == 'Q' || tab[i][j] == 'R') 166 | return (0); 167 | if (tab[i][j] == 'K') 168 | { 169 | ft_putstr("Success\n"); 170 | return (1); 171 | } 172 | i++; 173 | j--; 174 | } 175 | i = y - 1; 176 | j = x + 1; 177 | //en haut a droite; 178 | while (i >= 0) 179 | { 180 | if (tab[i][j] == 'P' || tab[i][j] == 'B' || tab[i][j] == 'Q' || tab[i][j] == 'R') 181 | return (0); 182 | if (tab[i][j] == 'K') 183 | { 184 | ft_putstr("Success\n"); 185 | return (1); 186 | } 187 | i--; 188 | j++; 189 | } 190 | i = y - 1; 191 | j = x - 1; 192 | //en haut a gauche 193 | while (i >= 0) 194 | { 195 | if (tab[i][j] == 'P' || tab[i][j] == 'B' || tab[i][j] == 'Q' || tab[i][j] == 'R') 196 | return (0); 197 | if (tab[i][j] == 'K') 198 | { 199 | ft_putstr("Success\n"); 200 | return (1); 201 | } 202 | i--; 203 | j--; 204 | } 205 | return (0); 206 | } 207 | 208 | int check_queen(char **tab, int i, int j) 209 | { 210 | if (check_bishop(tab, i, j) == 1) 211 | return (1); 212 | if (check_rook(tab, i, j) == 1) 213 | return (1); 214 | return (0); 215 | } 216 | 217 | int main(int argc, char **argv) 218 | { 219 | char **tab; 220 | int i = 0; 221 | int j = 0; 222 | if (argc > 1) 223 | { 224 | tab = create_tab(argc - 1, argv); 225 | if (tab == NULL) 226 | return (-1); 227 | while (tab[i] != NULL) 228 | { 229 | printf("tab[i] : %s\n", tab[i]); 230 | i++; 231 | } 232 | i = 0; 233 | while (tab[i] != NULL) 234 | { 235 | j = 0; 236 | while (tab[i][j] != '\0') 237 | { 238 | if (tab[i][j] == 'R') 239 | { 240 | if (check_rook(tab, i, j) == 1) 241 | return (0); 242 | } 243 | else if (tab[i][j] == 'P') 244 | { 245 | if (check_pawn(tab, i, j) == 1) 246 | return (0); 247 | } 248 | else if (tab[i][j] == 'B') 249 | { 250 | if (check_bishop(tab, i, j) == 1) 251 | return (0); 252 | } 253 | else if (tab[i][j] == 'Q') 254 | { 255 | if (check_queen(tab, i, j) == 1) 256 | return (0); 257 | } 258 | j++; 259 | } 260 | i++; 261 | } 262 | ft_putstr("Fail"); 263 | free(*tab); 264 | } 265 | ft_putstr("\n"); 266 | return (0); 267 | } --------------------------------------------------------------------------------