This repository was archived by the owner on Apr 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuerying the Document.c
308 lines (220 loc) · 6.7 KB
/
Querying the Document.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<assert.h>
#define MAX_CHARACTERS 1005
#define MAX_PARAGRAPHS 5
/**********************************************************************/
/**********************************************************************/
char* kth_word_in_mth_sentence_of_nth_paragraph(char**** document, int k, int m, int n)
{
return document[n-1][m-1][k-1];
}
/**********************************************************************/
char** kth_sentence_in_mth_paragraph(char**** document, int k, int m)
{
return document[m-1][k-1];
}
/**********************************************************************/
char*** kth_paragraph(char**** document, int k)
{
return document[k-1];
}
/**********************************************************************/
char* get_word(char* text, int start, int end)
{
int w_indx = 0 ;
/*
- if the string == "Ali" so its size equals to 4 bytes.
- 3 of them are for 'A', 'l' and 'i' the fourth one is for the '\0'.
- The string starts from index 0 and ends at index 2.
- So, if we want to add two to the difference between
the end and the start, to get the actual size of the string to allocate.
*/
char* word = calloc( end - start + 2, sizeof(char) ) ;
for( int i = start; i <= end; i++ )
{
word[w_indx] = text[i] ;
w_indx++ ;
}
/* Adding the NULL char at the end of each */
word[w_indx] = '\0' ;
return word ;
}
/**********************************************************************/
char** get_sentence(char* text, int start, int end)
{
char** sentence ;
int word_cntr = 1 ;
int i = start ;
int s_indx = 0 ;
int w_start = start ;
for( int j = start; j <= end; j++ )
{
if( text[j] == ' ' )
{
word_cntr++ ;
}
else
{
}
}
sentence = calloc( word_cntr, sizeof(char *) ) ;
for( i = start; i <= end; i++ )
{
if( text[i] == ' ' )
{
/* The sentence ends before the '.' char by one index */
sentence[s_indx] = get_word( text, w_start, i - 1 ) ;
s_indx++ ;
/* The next sentence start index */
w_start = i + 1 ;
}
else
{
}
}
/* Adding the last word to the sentence */
sentence[s_indx] = get_word( text, w_start, i - 1 ) ;
return sentence ;
}
/**********************************************************************/
char*** get_paragraph(char* text, int start, int end)
{
char*** paragraph ;
int sentence_cntr = 1 ;
int i = start ;
int p_indx = 0 ;
int s_start = start ;
for( int j = start; j <= end; j++ )
{
if( text[j] == '.' )
{
sentence_cntr++ ;
}
else
{
}
}
paragraph = calloc( sentence_cntr, sizeof(char **) ) ;
for( i = start; i <= end; i++ )
{
if( text[i] == '.' )
{
/* The sentence ends before the '.' char by one index */
paragraph[p_indx] = get_sentence( text, s_start, i - 1 ) ;
p_indx++ ;
/* The next sentence start index */
s_start = i + 1 ;
}
else
{
}
}
return paragraph ;
}
/**********************************************************************/
char**** get_document(char* text)
{
char **** docx ;
int paragraph_cntr = 1 ;
int i = 0 ;
int d_indx = 0 ;
int p_start = 0 ;
for( int j = 0; text[j] != '\0'; j++ )
{
if( text[j] == '\n' )
{
paragraph_cntr++ ;
}
else
{
}
}
docx = calloc( paragraph_cntr, sizeof(char***) ) ;
for( i = 0; text[i] != '\0'; i++ )
{
if( text[i] == '\n' )
{
/* The paragraph ends before the '\n' char by one index */
docx[d_indx] = get_paragraph( text, p_start, i - 1 ) ;
d_indx++ ;
/* The next paragraph start index */
p_start = i + 1 ;
}
else
{
}
}
/* Adding the last paragraph to the document */
docx[d_indx] = get_paragraph( text, p_start, i - 1 ) ;
return docx ;
}
/**********************************************************************/
/**********************************************************************/
char* get_input_text() {
int paragraph_count;
scanf("%d", ¶graph_count);
char p[MAX_PARAGRAPHS][MAX_CHARACTERS], doc[MAX_CHARACTERS];
memset(doc, 0, sizeof(doc));
getchar();
for (int i = 0; i < paragraph_count; i++) {
scanf("%[^\n]%*c", p[i]);
strcat(doc, p[i]);
if (i != paragraph_count - 1)
strcat(doc, "\n");
}
char* returnDoc = (char*)malloc((strlen (doc)+1) * (sizeof(char)));
strcpy(returnDoc, doc);
return returnDoc;
}
void print_word(char* word) {
printf("%s", word);
}
void print_sentence(char** sentence) {
int word_count;
scanf("%d", &word_count);
for(int i = 0; i < word_count; i++){
printf("%s", sentence[i]);
if( i != word_count - 1)
printf(" ");
}
}
void print_paragraph(char*** paragraph) {
int sentence_count;
scanf("%d", &sentence_count);
for (int i = 0; i < sentence_count; i++) {
print_sentence(*(paragraph + i));
printf(".");
}
}
int main()
{
char* text = get_input_text();
char**** document = get_document(text);
int q;
scanf("%d", &q);
while (q--) {
int type;
scanf("%d", &type);
if (type == 3){
int k, m, n;
scanf("%d %d %d", &k, &m, &n);
char* word = kth_word_in_mth_sentence_of_nth_paragraph(document, k, m, n);
print_word(word);
}
else if (type == 2){
int k, m;
scanf("%d %d", &k, &m);
char** sentence = kth_sentence_in_mth_paragraph(document, k, m);
print_sentence(sentence);
}
else{
int k;
scanf("%d", &k);
char*** paragraph = kth_paragraph(document, k);
print_paragraph(paragraph);
}
printf("\n");
}
}