diff options
| author | Charles <sircharlesaze@gmail.com> | 2019-07-26 23:17:36 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2019-07-26 23:17:36 +0200 |
| commit | 264ae2f047a53ffac92271e4609038d17605738e (patch) | |
| tree | 480c68814f822439850029df0e0249a2cafb8177 /exam_final/rendu/ord_alphlong | |
| parent | ad9867052d496f2c2a7f120a512208fb4dd4e787 (diff) | |
| download | piscine-264ae2f047a53ffac92271e4609038d17605738e.tar.gz piscine-264ae2f047a53ffac92271e4609038d17605738e.tar.bz2 piscine-264ae2f047a53ffac92271e4609038d17605738e.zip | |
exam final
Diffstat (limited to 'exam_final/rendu/ord_alphlong')
| -rwxr-xr-x | exam_final/rendu/ord_alphlong/helper.c | 52 | ||||
| -rwxr-xr-x | exam_final/rendu/ord_alphlong/helper2.c | 77 | ||||
| -rwxr-xr-x | exam_final/rendu/ord_alphlong/helper3.c | 75 | ||||
| -rwxr-xr-x | exam_final/rendu/ord_alphlong/include.h | 42 | ||||
| -rwxr-xr-x | exam_final/rendu/ord_alphlong/main.c | 32 |
5 files changed, 278 insertions, 0 deletions
diff --git a/exam_final/rendu/ord_alphlong/helper.c b/exam_final/rendu/ord_alphlong/helper.c new file mode 100755 index 0000000..7d285c1 --- /dev/null +++ b/exam_final/rendu/ord_alphlong/helper.c @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* helper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: exam <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/26 14:40:48 by exam #+# #+# */ +/* Updated: 2019/07/26 16:35:08 by exam ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <stdlib.h> +#include <unistd.h> +#include "include.h" + +int ft_strlen(char *str) +{ + int counter; + + counter = 0; + while (str[counter]) + counter++; + return (counter); +} + +int letter_diff(char a, char b) +{ + if (b >= 'A' && b <= 'Z') + b += 'a' - 'A'; + if (a >= 'A' && a <= 'Z') + a += 'a' - 'A'; + return (a - b); +} + +void ft_putstr(char *str) +{ + while (*str) + write(1, str++, 1); +} + +int str_lettercmp(char *s1, char *s2) +{ + if (ft_strlen(s1) != ft_strlen(s2)) + return (-1); + while (*s1 && *s2 && letter_diff(*s1, *s2) == 0) + { + s1++; + s2++; + } + return (letter_diff(*s1, *s2)); +} diff --git a/exam_final/rendu/ord_alphlong/helper2.c b/exam_final/rendu/ord_alphlong/helper2.c new file mode 100755 index 0000000..431274b --- /dev/null +++ b/exam_final/rendu/ord_alphlong/helper2.c @@ -0,0 +1,77 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* helper2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: exam <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/26 15:20:07 by exam #+# #+# */ +/* Updated: 2019/07/26 16:29:42 by exam ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <stdlib.h> +#include "include.h" + +int sorted(char **strs, int cmp(char*, char*)) +{ + int i; + + i = 0; + if (strs[0] == NULL) + return (1); + while (strs[i + 1] != NULL) + { + if ((*cmp)(strs[i], strs[i + 1]) > 0) + return (0); + i++; + } + return (1); +} + +void sort_strs(char **strs, int cmp(char*, char*)) +{ + int i; + char *tmp; + + while (!sorted(strs, cmp)) + { + i = 0; + while (strs[i + 1] != NULL) + { + if ((*cmp)(strs[i], strs[i + 1]) > 0) + { + tmp = strs[i]; + strs[i] = strs[i + 1]; + strs[i + 1] = tmp; + } + i++; + } + } +} + +int strlen_cmp(char *s1, char *s2) +{ + return (ft_strlen(s1) - ft_strlen(s2)); +} + +void print_strs(char **strs) +{ + int i; + int len; + + i = 0; + while (strs[i] != NULL) + { + len = ft_strlen(strs[i]); + while (strs[i] && ft_strlen(strs[i]) == len) + { + ft_putstr(strs[i]); + if (strs[i + 1] != NULL && ft_strlen(strs[i + 1]) == len) + ft_putstr(" "); + else + ft_putstr("\n"); + i++; + } + } +} diff --git a/exam_final/rendu/ord_alphlong/helper3.c b/exam_final/rendu/ord_alphlong/helper3.c new file mode 100755 index 0000000..83b548a --- /dev/null +++ b/exam_final/rendu/ord_alphlong/helper3.c @@ -0,0 +1,75 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* helper3.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: exam <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/26 16:34:01 by exam #+# #+# */ +/* Updated: 2019/07/26 16:34:28 by exam ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <stdlib.h> +#include "include.h" + +int in_charset(char c) +{ + return (c == ' ' || c == '\t'); +} + +int count_segment(char *str) +{ + int counter; + + counter = 0; + while (*str) + { + if (in_charset(*str)) + { + str++; + continue ; + } + counter++; + while (*str && !in_charset(*str)) + str++; + } + return (counter); +} + +char **ft_split(char *str) +{ + char **strs; + char *tmp; + int size; + int i; + int j; + + size = count_segment(str); + if ((strs = (char**)malloc(sizeof(char*) * (size + 1))) == NULL) + return (NULL); + i = 0; + while (i < size) + { + if (*str && in_charset(*str)) + { + str++; + continue ; + } + j = 0; + while (str[j] && !in_charset(str[j])) + j++; + if ((tmp = (char*)malloc(sizeof(char) * (j + 1))) == NULL) + return (NULL); + j = 0; + while (*str && !in_charset(*str)) + { + tmp[j] = *str++; + j++; + } + tmp[j] = '\0'; + strs[i++] = tmp; + } + strs[i] = NULL; + return (strs); +} diff --git a/exam_final/rendu/ord_alphlong/include.h b/exam_final/rendu/ord_alphlong/include.h new file mode 100755 index 0000000..ac171fa --- /dev/null +++ b/exam_final/rendu/ord_alphlong/include.h @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* include.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: exam <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/26 14:44:30 by exam #+# #+# */ +/* Updated: 2019/07/26 16:34:49 by exam ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef INCLUDE_H +# define INCLUDE_H + +/* +** helper.c +*/ + +int ft_strlen(char *str); +int letter_diff(char a, char b); +int str_lettercmp(char *s1, char *s2); +void ft_putstr(char *str); + +/* +** helper2.c +*/ + +int sorted(char **strs, int cmp(char*, char*)); +void sort_strs(char **strs, int cmp(char*, char*)); +int strlen_cmp(char *s1, char *s2); +void print_strs(char **strs); + +/* +** helper3.c +*/ + +int in_charset(char c); +int count_segment(char *str); +char **ft_split(char *str); + +#endif diff --git a/exam_final/rendu/ord_alphlong/main.c b/exam_final/rendu/ord_alphlong/main.c new file mode 100755 index 0000000..5a46015 --- /dev/null +++ b/exam_final/rendu/ord_alphlong/main.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: exam <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/26 14:37:24 by exam #+# #+# */ +/* Updated: 2019/07/26 16:33:22 by exam ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include <unistd.h> +#include <stdlib.h> +#include "include.h" + +#include <stdio.h> +int main(int argc, char **argv) +{ + char **strs; + + if (argc != 2) + { + write(1, "\n", 1); + return (0); + } + strs = ft_split(argv[1]); + sort_strs(strs, &strlen_cmp); + sort_strs(strs, &str_lettercmp); + print_strs(strs); + return (0); +} |
