You are viewing a single comment's thread. Return to all comments →
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]; } void read_word(char** pool, char*** words, char* beg, char* end) { int len = end - beg; memcpy(*pool, beg, len); *(*words)++ = *pool; *(*pool + len + 1) = '\0'; *pool += len + 1; } char**** get_document(char* text) { int num_paras = 0; int num_sens = 0; int num_words = 0; int text_size = 0; for (char *p = text, c = *p; c; c = *++p) { switch (c) { case '\n': ++num_paras; break; case '.': ++num_sens; case ' ': ++num_words; default: ++text_size; } } char**** paras = malloc(num_paras * sizeof(char***)), ****doc = paras; char*** sens = malloc(num_sens * sizeof(char**)); char** words = malloc(num_words * sizeof(char*)); char* pool = malloc(text_size); char* word = NULL; *sens = words; *paras = sens; for (char* p = text, c = *p; c; c = *++p) { if (c == '\n') *++paras = sens; else if (c == ' ' || c == '.') { read_word(&pool, &words, word, p); if (c == '.' && *(p + 1)) *++sens = words; word = NULL; } else if (word == NULL) word = p; } return doc; }
Seems like cookies are disabled on this browser, please enable them to open this website
Querying the Document
You are viewing a single comment's thread. Return to all comments →