-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQuerying_the_document.c
More file actions
46 lines (39 loc) · 1.32 KB
/
Copy pathQuerying_the_document.c
File metadata and controls
46 lines (39 loc) · 1.32 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
char**** get_document(char* text)
{
char ****document;
int paragraphs = 1, sentences = 1, words = 1, i;
document = (char ****)malloc(sizeof(char ***));
document[0] = (char ***)malloc(sizeof(char **));
document[0][0] = (char **)malloc(sizeof(char *));
document[0][0][0] = text;
for(i=0; text[i+1] != 0; i++)
{
if(text[i+1] == '\n')
text[i++] = 0;
switch(text[i])
{
case '\n':
document = (char ****)realloc(document, ++paragraphs * sizeof(char ***));
sentences = 0;
case '.':
document[paragraphs-1] = (char ***)realloc(document[paragraphs-1], ++sentences * sizeof(char **));
words = 0;
case ' ':
document[paragraphs-1][sentences-1] = (char **)realloc(document[paragraphs-1][sentences-1], ++words * sizeof(char *));
document[paragraphs-1][sentences-1][words-1] = &text[i+1];
text[i] = 0;
break;
}
}
text[i] = 0;
return document;
}
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];
}