-
Notifications
You must be signed in to change notification settings - Fork 0
/
36__ft_write_first_word.C
97 lines (71 loc) · 2.46 KB
/
36__ft_write_first_word.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
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
Write the function ft_write_first_word that a string as an argument
The function must write the first string present in its argument on standard output stream, followed by a newline
If its argument is a null pointer, it must write "Error", followed by a newline on standard error stream, followed by a newline
A "word" is the part of the string delimited by its start, its end and all the characters for which isspace function returns non-zero
The function must be prototyped as follows: void ft_write_first_word(char *str);
Allowed functions: write
Examples:
ft_write_first_word("Bonjour"); //will write "Bonjour\n" on standard output stream
ft_write_first_word("Bon jour"); //will write "Bon\n" on standard output stream
ft_write_first_word("Bon\tjour"); //will write "Bon\n" on standard output stream
ft_write_first_word(""); //will write "\n" on standard output stream
ft_write_first_word(" "); //will write "\n" on standard output stream
ft_write_first_word("\r\r\r\r"); //will write "\n" on standard output stream
ft_write_first_word(" Bon jour"); //will write "Bon\n" on standard output stream
ft_write_first_word(" Bonjour"); //will write "Bonjour\n" on standard output stream
ft_write_first_word("NULL"); //will write "Error\n" on standard error stream
*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
void ft_putchar(char c)
{
write(1, &c, 1);
}
int ft_isspace(char c)
{
if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\v' || c == '\f')
{
return (1);
}
return (0);
}
void ft_write_first_word(char *str)
{
int i;
i = 0;
if (str == NULL)
{
write(2, "Error\n", 7);
return;
}
else
{
while ((ft_isspace(str[i]) == 1) && str[i] != '\0') // ft_isspace returns 1
{
i++;
}
while ((ft_isspace(str[i]) == 0) && str[i] != '\0') // ft_isspace returns 0
{
ft_putchar(str[i]);
i++;
}
}
write(1, "\n", 1);
}
/*
int main()
{
ft_write_first_word("Bonjour");
ft_write_first_word("Bon jour");
ft_write_first_word("Bon\tjour");
ft_write_first_word("");
ft_write_first_word(" ");
ft_write_first_word("\r\r\r\r");
ft_write_first_word(" Bon jour");
ft_write_first_word(" Bonjour");
ft_write_first_word(NULL);
return (0);
}
*/