-
Notifications
You must be signed in to change notification settings - Fork 3
/
3_epur_str.c
84 lines (73 loc) · 1.82 KB
/
3_epur_str.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
Assignment name : epur_str
Expected files : epur_str.c
Allowed functions: write
--------------------------------------------------------------------------------
Write a program that takes a string, and displays this string with exactly one
space between words, with no spaces or tabs either at the beginning or the end,
followed by a \n.
A "word" is defined as a part of a string delimited either by spaces/tabs, or
by the start/end of the string.
If the number of arguments is not 1, or if there are no words to display, the
program displays \n.
Example:
$> ./epur_str "vous voyez c'est facile d'afficher la meme chose" | cat -e
vous voyez c'est facile d'afficher la meme chose$
$> ./epur_str " seulement la c'est plus dur " | cat -e
seulement la c'est plus dur$
$> ./epur_str "comme c'est cocasse" "vous avez entendu, Mathilde ?" | cat -e
$
$> ./epur_str "" | cat -e
$
$>
*/
#include <unistd.h>
#define is_space(c) (c == ' ' || c == '\t')
#define is_char(c) (c && !is_space(c))
int main(int ac, char **av)
{
char *word;
unsigned len;
if (ac == 2)
{
word = av[1];
while (is_space(*word)) /** leading space */
word++;
while (*word)
{
len = 0;
while (is_char(*(word + len)))
len++;
if (len)
word += write(1, word, len);
while (is_space(*word))
word++;
if (*word)
write(1, " ", 1);
}
}
write(1, "\n", 1);
return (0);
}
/** Evilcat(Evilcat325) 's solution(https://github.com/Evilcat325)
int main(int ac, char **av)
{
char *word;
unsigned len;
len = 0;
if (ac == 2)
{
word = av[1];
while (*word)
if (is_char(*word) && !(len = 0))
while (is_char(*(word)) && word++ && ++len)
len = is_char(*(word)) ? len : write(1, word - len, len);
else if (len && is_char(*(word + 1)) && word++)
write(1, " ", 1);
else
word++;
}
write(1, "\n", 1);
return (0);
}
*/