-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrostring.c
66 lines (57 loc) · 1.07 KB
/
rostring.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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
static int size_of_word(char *s)
{
int i;
for (i = 0; s[i] && !strchr(" \t", s[i]); ++i);
return (i);
}
static int count_words(char *s)
{
int i;
int count;
for (i = count = 0; (unsigned)i < strlen(s); i += size_of_word(s + i), ++count)
while (strchr(" \t", s[i]))
++i;
return (count);
}
static void display(char **tab)
{
int i;
for (i = 1; tab[i]; ++i)
printf("%s ", tab[i]);
printf("%s\n", *tab);
}
static int parse(char *s)
{
char **tab;
int words;
int i;
int j;
words = count_words(s);
if (!(tab = malloc((sizeof(char *) * (1 + words)))))
return (1);
tab[words] = NULL;
i = -1;
j = -1;
while (s[++i])
{
if (!i || ((s[i - 1] == 0 || strchr(" \t", s[i - 1]))
&& !strchr(" \t", s[i])))
tab[++j] = s + i;
else if (strchr(" \t", s[i]))
s[i] = 0;
}
display(tab);
free(tab);
return (0);
}
int main(int ac, char **av)
{
if (ac != 2)
return (printf("\n"));
while (strchr(" \t", *av[1]))
++av[1];
return (parse(av[1]));
}