-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit.c
158 lines (133 loc) · 2.77 KB
/
split.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "main.h"
#include <stdlib.h>
int lengthOfNextWord(char *string);
int countWords(char *string);
int isQuote(char c);
/**
* split - splits a string delimited by ' '
* @string: string pointer
*
* Return: an array of malloc'd strings
*/
char **split(char *string)
{
int number_of_words = countWords(string);
char *word;
char **words, **s;
int word_len = 0;
word = NULL;
words = s = NULL;
if (number_of_words == 0)
return (NULL);
/* Allocate space for words and null pointer*/
words = malloc(sizeof(char *) * (number_of_words + 1));
s = words;
if (words == NULL)
return (NULL);
while (*string && *string != '#')
{
if (*string != ' ')
{
/* Allocate enough space the word and a null byte*/
word_len = lengthOfNextWord(string);
word = malloc(sizeof(char) * (word_len + 1));
if (!word)
return (NULL);
/*
* If current character is a quote, copy only characters
* inside of the quotes
*/
if (isQuote(*string))
{
_strncpy(word, (string + 1), word_len);
/* Add 2 to jump to account for the two quotes */
word_len += 2;
}
else
_strncpy(word, string, word_len);
string += word_len;
*s = word;
s++;
}
string++;
}
words[number_of_words] = NULL;
return (words);
}
/**
* lengthOfNextWord - gets the length of the next word in a string
* @string: string pointer
*
* Return: length of the next word in the string pointer
*/
int lengthOfNextWord(char *string)
{
int i = 0;
/* Handle words in double quotes e.g "hello world" */
if (isQuote(*string))
{
/*
* The reason for adding 1 is to start counting after the
* quote
* Count until the closing quote is reached
*/
while (*(string + i + 1) && !isQuote(*(string + i + 1)))
i++;
}
else
{
/* Count until a blank space is reached */
while (*(string + i) && *(string + i) != ' ')
i++;
}
return (i);
}
/**
* countWords - returns the total number of words in the string
* @string: string pointer
*
* Return: the number of words in the string pointer
*/
int countWords(char *string)
{
int words = 0;
int inWord = 0;
int inQuotes = 0;
while (*string && *string != '#')
{
/* Character is the first character in a word */
if (*string != ' ' && inWord == 0)
{
if (isQuote(*string))
inQuotes = 1;
words++;
inWord = 1;
}
/* Character is a blank space outside of quotes*/
else if (*string == ' ' && !inQuotes)
{
inWord = 0;
}
/* Character is a closing quote */
else if (isQuote(*string) && inQuotes)
{
/* set inWord to false & inQuote to false*/
inWord = 0;
inQuotes = 0;
}
string++;
}
return (words);
}
/**
* isQuote - checks if a character is a quote
* @c: character
*
* Return: 1 if quote otherwise zero
*/
int isQuote(char c)
{
if (c == '\'' || c == '"')
return (1);
return (0);
}