You are viewing a single comment's thread. Return to all comments →
C Solution :
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_document(char* text) { short i = 0, j = 0, pr_size = 0, sn_size[5] = {0}, wr_size[5][20] = {0}; char* token; /********* Tokenising the Paragraphs *********/ char** paragraphs = (char**)malloc(5 * sizeof(char**)); if(paragraphs == NULL) { fprintf(stderr, "Memory allocation failed at step #1!"); return NULL; } token = strtok(text, "\n"); while(token != NULL) { paragraphs[i] = strdup(token); token = strtok(NULL, "\n"), i++, pr_size++; } /********* Tokenising the Sentences *********/ char*** sentences = (char***)malloc(5 * sizeof(char**)); if(sentences == NULL) { fprintf(stderr, "Memory allocation failed at step #2.1!"); return NULL; } for(i = 0; i < pr_size; i++) { sentences[i] = (char**)malloc(10 * sizeof(char*)); if(sentences[i] == NULL) { fprintf(stderr, "Memory allocation failed at step #2.2!"); return NULL; } token = strtok(paragraphs[i], "."), j = 0; while(token != NULL) { sentences[i][j] = (char*)malloc((strlen(token) + 1) * sizeof(char)); if(sentences[i][j] == NULL) { fprintf(stderr, "Memory allocation failed at step #2.3!"); return NULL; } strcpy(sentences[i][j], token); token = strtok(NULL, "."); j++, sn_size[i]++; } } /********* Creating the Document *********/ char**** document = (char****)malloc(pr_size * sizeof(char***)); if(document == NULL) { fprintf(stderr, "Memory allocation failed at step #3.1!"); return NULL; } /********* Creating Paragraphs *********/ for(i = 0; i < pr_size; i++) { document[i] = (char***)malloc(sn_size[i] * sizeof(char**)); if(document[i] == NULL) { fprintf(stderr, "Memory allocation failed at step #3.2!"); return NULL; } /********* Creating Sentences *********/ for(j = 0; j < sn_size[i]; j++) { document[i][j] = (char**)malloc(100 * sizeof(char*)); if(document[i][j] == NULL) { fprintf(stderr, "Memory allocation failed at step #3.3!"); return NULL; } token = strtok(sentences[i][j], " ."); short k = wr_size[i][j]; while(token != NULL) { document[i][j][k] = (char*)malloc((strlen(token) + 1) * sizeof(char)); if(document[i][j][k] == NULL) { fprintf(stderr, "Memory allocation failed at step #3.4!"); return NULL; } strcpy(document[i][j][k], token); token = strtok(NULL, " ."); k++; } } } return document; }
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 →
C Solution :