-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken.c
More file actions
116 lines (97 loc) · 2.07 KB
/
token.c
File metadata and controls
116 lines (97 loc) · 2.07 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "shell.h"
/**
* **strtow - this function count the number of word in the poput string by
* iterating through it
* @str: function parameter showing the input string
* @d: function parameter showing d as a delimeter string
* Return: 0 if successful
*/
char **strtow(char *str, char *d)
{
int a;
int j;
int e;
int m;
int numwords = 0;
char **s;
if (str == NULL || str[0] == 0)
return (NULL);
if (!d)
d = " ";
for (a = 0; str[a] != '\0'; a++)
if (!is_delim(str[a], d) && (is_delim(str[a + 1], d) || !str[a + 1]))
numwords++;
if (numwords == 0)
return (NULL);
s = malloc((numwords + 1) * sizeof(char *));
if (!s)
return (NULL);
for (a = 0, j = 0; j < numwords; j++)
{
while (is_delim(str[a], d))
a++;
e = 0;
while (!is_delim(str[a + e], d) && str[a + e] != '\0')
e++;
s[j] = malloc((e + 1) * sizeof(char));
if (!s[j])
{
for (e = 0; e < j; e++)
free(s[e]);
free(s);
return (NULL);
}
for (m = 0; m < e; m++)
s[j][m] = str[a++];
s[j][m] = 0;
}
s[j] = NULL;
return (s);
}
/**
* **strtow2 - this function has similar function but uses a single char as a
* delimiter instead of a delimiter string
* @str: function parameter showing the input string
* @d: funnction parameter indicating the delimeter
* Return: 0 if successful
*/
char **strtow2(char *str, char d)
{
int a;
int j;
int f;
int e;
int numwords = 0;
char **s;
if (str == NULL || str[0] == 0)
return (NULL);
for (a = 0; str[a] != '\0'; a++)
if ((str[a] != d && str[a + 1] == d) || (str[a] != d && !str[a + 1]) || str[a + 1] == d)
numwords++;
if (numwords == 0)
return (NULL);
s = malloc((numwords + 1) * sizeof(char *));
if (!s)
return (NULL);
for (a = 0, j = 0; j < numwords; j++)
{
while (str[a] == d && str[a] != d)
a++;
f = 0;
while (str[a + f] != d && str[a + f] && str[a + f] != d)
f++;
s[j] = malloc((f + 1) * sizeof(char));
if (!s[j])
{
for (f = 0; f < j; f++)
free(s[f]);
free(s);
return (NULL);
}
for (e = 0; e < f; e++)
s[j][e] = str[a++];
s[j][e] = 0;
}
s[j] = NULL;
return (s);
}