Created
January 10, 2018 13:19
-
-
Save msabwat/0e76576f094f89f17ad9a4807cfad0ad to your computer and use it in GitHub Desktop.
Fonction de l'examen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ************************************************************************** */ | |
/* */ | |
/* ::: :::::::: */ | |
/* rostring.c :+: :+: :+: */ | |
/* +:+ +:+ +:+ */ | |
/* By: msabwat <[email protected]> +#+ +:+ +#+ */ | |
/* +#+#+#+#+#+ +#+ */ | |
/* Created: 2018/01/09 10:04:25 by exam #+# #+# */ | |
/* Updated: 2018/01/09 13:49:33 by exam ### ########.fr */ | |
/* */ | |
/* ************************************************************************** */ | |
#include <unistd.h> | |
#include <stdlib.h> | |
void ft_putchar(char c) | |
{//affiche un char | |
write(1, &c, 1);//appel système pour afficher un char | |
} | |
void ft_putstr(char *str) | |
{//affiche une chaine de charactère | |
int i = 0; | |
while (str[i]) | |
{ | |
ft_putchar(str[i]); | |
i++; | |
} | |
} | |
int ft_strlen(char *str) | |
{//Retourne la longueur d'une chaine | |
int i = 0; | |
while (str[i]) | |
i++; | |
return (i); | |
} | |
int isdelim(char c) | |
{//Retourne 0 si il y a un délimiteur, 1 sinon | |
if (c == ' ' || c == '\t') | |
return (1); | |
return (0); | |
} | |
char *get_first_word(char *str) | |
{//Retourne une chaine de charactère avec mémoire allouée dynamiquement, représentant le premier mot | |
int i = 0; | |
int ind; | |
char *temp; | |
static int start; | |
int cnt = 0; | |
while (isdelim(str[i])) | |
i++; | |
start = i; | |
if (i == 0) | |
start = 0; | |
while(!(isdelim(str[i]))) | |
{ | |
cnt++; | |
i++; | |
} | |
if(!(temp = malloc(sizeof(char) * (cnt + 1)))) | |
return (NULL); | |
temp[cnt] = '\0'; | |
ind = 0; | |
while (ind < cnt) | |
{ | |
temp[ind] = str[start]; | |
ind++; | |
start++; | |
} | |
return (temp); | |
} | |
char *ft_decal(char *decal, int ind) | |
{//Fonction qui retourne la chaine de char. sans le premier mot. En fonction de l'index de fin du premier mot. | |
int i = 0; | |
while (i < ind) | |
{ | |
decal++; | |
i++; | |
} | |
return (decal); | |
} | |
char *onesp(char *str) | |
{//Fonction qui transforme une chaine de char. en une chaine de char. séparée par des espaces | |
char *temp; | |
int i = 0; | |
int j = 0; | |
if (!(temp = malloc(sizeof(char) * (ft_strlen(str) + 1)))) | |
return(NULL); | |
while (isdelim(str[j])) | |
j++; | |
while (str[j]) | |
{ | |
if (isdelim(str[j])) | |
{ | |
temp[i] = str[j]; | |
i++; | |
j++; | |
while (isdelim(str[j])) | |
j++; | |
} | |
if (str[j]) | |
{ | |
temp[i] = str[j]; | |
i++; | |
j++; | |
} | |
if (isdelim(str[j - 1])) | |
temp[i - 1] = '\0'; | |
temp[i] = '\0'; | |
} | |
return (temp); | |
} | |
char *ft_strjoin(char *s1, char *s2) | |
{//Fonction qui regroupe deux chaines de char. avec un espace entre les deux | |
char *result; | |
int size = ft_strlen(s1) + ft_strlen(s2); | |
int i = 0; | |
if(!(result = malloc(sizeof(char) * (size + 2)))) | |
return (NULL); | |
result[size + 1] = '\0'; | |
while (*s2) | |
{ | |
result[i] = *s2; | |
s2++; | |
i++; | |
} | |
result[i] = ' '; | |
i++; | |
while (*s1) | |
{ | |
result[i] = *s1; | |
s1++; | |
i++; | |
} | |
return(result); | |
} | |
int nb_words(char *str) | |
{//Compte le nombre de mots dans une chaine de char. | |
int i = 0; | |
int cnt = 0; | |
while (str[i]) | |
{ | |
if (i != 0 && (isdelim(str[i]) && (!isdelim(str[i - 1])))) | |
cnt++; | |
i++; | |
} | |
if (str[i] == '\0' && (!isdelim(str[i - 1]))) | |
cnt++; | |
return (cnt); | |
} | |
char *rostring(char *str) | |
{//fonction qui produit le résultat escompté. | |
int i = 0; | |
int start = 0; | |
char *result; | |
if (nb_words(str) == 1) | |
return (onesp(str)); | |
while (isdelim(str[i])) | |
i++; | |
start = i; | |
if (i == 0) | |
start = 0; | |
char *first_wd = get_first_word(str); | |
char *rest = onesp(ft_decal(str, start + ft_strlen(first_wd))); | |
result = ft_strjoin(first_wd, rest); | |
return (result); | |
} | |
int main(int ac, char **av) | |
{ | |
if (ac > 1) | |
ft_putstr(rostring(av[1])); | |
ft_putchar('\n'); | |
return (0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment